I am posting this more to inform others than ask a question, since I find this to be rather valuable. A search on Wikipedia led me to an AppleScript article that explained if you right click in Script Editor you get a list of options like automatically inserting comments, or tell blocks, or any other snippet of code you might regularly use. Furthermore, you can add and edit scripts in this menu - just go to the “Script Editor Scripts” folder of your “Scripts” Folder in your Library.
I feel extremely silly now for not trying a Right Click in the editor window earlier…
jON bEEBE
I’ll join you in that feeling. I never noticed that feature either, nor did I know that I could add my own handlers. I’ve been copying and pasting.
jonn8
April 10, 2005, 2:21am
#3
Yes, this is a very handy feature. Here’s an extremely useful script I wrote for selecting a routine you’ve written and easily adding it as an insertion script in the Script Editor Scripts folder for future use:
–Open this script in a new Script Editor window.
property last_target_folder : “”
property script_type : “Insert Directly”
try
tell application “Script Editor” to tell front document to set the_routine to selection ’s contents
if the_routine = “” then return
set routine_name to (display dialog “Enter a name for this routine:” default answer “” buttons {“Cancel”, “OK”} default button 2 with icon 1)'s text returned
set script_type to (display dialog “When run, should this routine be placed on the clipboard or inserted directly into your script?” buttons {“Cancel”, “Clipboard”, “Insert Directly”} default button script_type with icon 1)'s button returned
set SE_scripts_folder to ((path to scripts folder from local domain ) as Unicode text ) & “Script Editor Scripts:” –maybe change to user domain depending on your system
try
tell application “System Events” to set SE_scripts_subfolders to name of folders of (SE_scripts_folder as alias )
if last_target_folder = “” then set last_target_folder to SE_scripts_subfolders ’s item 1
set target_folder to (choose from list SE_scripts_subfolders with prompt “Where should “” & routine_name & “” be saved?” default items {last_target_folder } OK button name “Save”) as Unicode text
if target_folder = “false” then return
set last_target_folder to target_folder
set target_folder to SE_scripts_folder & target_folder & “:”
on error
set target_folder to SE_scripts_folder
end try
set the_script to “script return_script
property routine_name : “” & routine_name & “”
property the_routine : " & my [color=green]escape_string/color
if script_type = “Insert Directly” then
set the_script to the_script & "
tell application “Script Editor” to tell front document to set contents of selection to ((get contents of selection) & the_routine)”
else
set the_script to the_script & "
set the clipboard to the_routine
tell application “Script Editor”
activate
display dialog “The routine “” & routine_name & “” has been placed on the clipboard. You may now paste it into a script window.” buttons {“•”} default button 1 with icon 1 giving up after 5
end tell"
end if
set the_script to the_script & "
end script
return return_script"
store script (run script the_script ) in ((target_folder & routine_name & “.scpt”) as file specification )
beep 2
display dialog ““” & routine_name & “” has been saved in “” & target_folder & “”” & return & return & “You may need to quit Script Editor to use this script from the contextual menu.” buttons {“OK”} default button 1 with icon 1
on error e
display dialog "ERROR: " & e buttons {“OK”} default button 1 with icon 0
end try
on [color=green]escape_string/color
set s to my snr (s , “", “\”)
set s to my snr (s , “””, “"”)
return “”" & s & “”"
end escape_string
on snr (t , s , r )
tell (a reference to my text item delimiters )
set {o , contents } to {contents , s }
set {t , contents } to {t ’s text items , r }
set {t , contents } to {“” & t , o }
end tell
return t
end snr
Save this as “Save Selection as Script…scpt” in your Script Editor Scripts folder and relaunch SE to access it. You may need to adjust the path to the scripts folder to your user’s script folder if that’s where you store your scripts (I keep them in the main library so I can access them from all users on my machine).
Jon
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]
That’s very cool! Jonn8!
Is there a way to integrate this in xCode?
You would need to add an extra ScriptSnippets menui tem to the standard xCode contextual menu, right?
Vince
Right after I found this feature I thought about writing a script that does exactly what yours does…Thanks!
jON bEEBE
this is why this bbs is so valuable to us`
keep up this good work gays
thanks JJ