I have just been using applescript for a few days. One of the things I liked about the script editor is that you could select lines in the script, context-click on the selection, and select Comment Tags to automatically turn the whole selection into a comment. And it seemed to work fine. But today while working on a script I got some errors and somehow the context menu is now completely different. I don’t remember just what was in the original menu other than Comment Tags, but the new menu contains: Search in Spotlight, Search in Google, Look up in Dictionary, Cut, Copy, Paste, Spelling, Font, Speech, Writing Direction, About these Scripts, Open Scripts Folder. No more Comment Tags. I have tried to quit the script editor and re-open it: same thing. I re-booted the Mac: same thing. I assume that it’s not possible to install a new version of Applescript. I assume that something got corrupted somewhere. Any suggestions.
Model: iMac
AppleScript: 1.10.7
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)
If you need it again the use this copy to make a new on
set CR to ASCII character 13
set NL to ASCII character 10
tell application "Script Editor"
tell front document
--tell document 2 --Debug mode
set the target_string to "--XXXX"
set the selected_text to contents of selection
if the selected_text is not "" then
if the selected_text contains "(*" and the selected_text contains "*)" then
--Could be a remove or another add
set BlockStart to the selected_text starts with "(*" & CR or the selected_text starts with "(*" & NL
set BlockEnd to the selected_text ends with "*)" & CR or the selected_text ends with "*)" & NL
if BlockStart and BlockEnd then
--Case as normally created by this script
set contents of selection to characters 4 through -4 of selected_text as text
else
set BlockStart to the selected_text starts with "(*"
set BlockEnd to the selected_text ends with "*)"
if BlockStart and BlockEnd then
--Block starts with "(*" exactly and ends with "*)" exactly
set contents of selection to characters 3 through -3 of selected_text as text
else
--find first occurrence of "(*"
set BlockStartOffset to offset of "(*" in selected_text
--find last occurrence of "*)"
set SelectionLength to length of selected_text
set BlockEndOffset to SelectionLength - ¬
(offset of ")*" in (reverse of (characters of selected_text) as text))
log {BlockStartOffset, BlockEndOffset, SelectionLength}
if BlockStartOffset is less than BlockEndOffset then
if BlockStart then
--Block starts with "(*", but "*)" is somewhere before end of selection
set Newtext to (characters 3 through (BlockEndOffset - 1) of selected_text as text) & ¬
characters (BlockEndOffset + 2) through -1 of selected_text as text
set contents of selection to Newtext
else
if BlockEnd then
--Block ends with "*)", but "(*" is somewhere past beginning of selection
set Newtext to (characters 1 through (BlockStartOffset - 1) of selected_text as text) & ¬
characters (BlockStartOffset + 2) through -3 of selected_text as text
set contents of selection to Newtext
else
--Block start and end are not at the selection end points (extract three blocks of text)
set Newtext to ((characters 1 through (BlockStartOffset - 1) of selected_text as text) & ¬
characters (BlockStartOffset + 2) through (BlockEndOffset - 1) of selected_text as text) & ¬
characters (BlockEndOffset + 2) through -1 of selected_text as text
set contents of selection to Newtext
end if
end if
else
if true then
--Must be a request to add comment block
if the last character of selected_text is in {CR, NL} then
set contents of selection to "(*" & return & ¬
selected_text & ¬
"*)" & return
else
set contents of selection to return & ¬
"(*" & return & ¬
selected_text & return & ¬
"*)" & return
end if
else
--display dialog "Invalid block comment range selected." with icon stop buttons "OK" default button 1
end if
end if
end if
end if
else
--Must be a request to add comment block
if the last character of selected_text is in {CR, NL} then
set contents of selection to "(*" & return & ¬
selected_text & ¬
"*)" & return
else
set contents of selection to return & ¬
"(*" & return & ¬
selected_text & return & ¬
"*)" & return
end if
end if
else
display dialog "Select some text before invoking this script command." with icon stop buttons "OK" default button 1
end if
end tell
end tell
(*
on replace_and_select(target_string, replacement_string)
tell application "Script Editor"
tell the front document
set this_text to the contents
set this_offset to the offset of the target_string in this_text
if this_offset is not 0 then
set selection to characters this_offset thru (this_offset + (length of the target_string) - 1)
set the contents of the selection to the replacement_string
else
set selection to {}
end if
end tell
end tell
end replace_and_select
*)
(*
--Global remove is too extensive
set this_text to my replace_chars(selected_text, "(*", "")
set this_text to my replace_chars(this_text, "*)", "")
set the contents of the selection of the front document to this_text
*)
(*
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
*)
The scripts listed in Script Editor’s context menu are from /Library/Scripts/Script Editor Scripts. In AppleScript, you might say ((path to scripts folder from local domain as Unicode text) & “Script Editor Scripts”)
((path to scripts folder from local domain as Unicode text) & "Script Editor Scripts")
tell application "Finder" to open result
The “Open Scripts Folder” menu item should open the folder for you, as does the above AppleScript.
Maybe you accidentally selected “Open Scripts Folder” and then later delete the contents? Check your Trash. Or failing that recover them from your most recent unaffected backup. Or if you do not have a backup, first start doing backups regularly right now! (Seriously!) Second, there should be a way to extract these files from OS X install media, but I would have to poke around the find the specifics.
This accidentally-deleted-systemish-files scenario is one of the reasons I recommend that people use a non-admin user account for normal activity and only switch to an admin user (or authenticate as one) for special operations.
OK, If the files are missing and you do not have a backup, here is how you can reinstall them.
If you are comfortable with the command line, the tool pax will do the job. Point it at System/Installation/Packages/Essentials.pkg/Contents/Archive.pax.gz from your Mac OS X installation DVD and arrange for it to reinstall the files you need.
If using pax directly is too daunting, you could use something like Pacifist to reinstall just the /Library/Script/Script Editor Scripts directory:
Run Pacifist.
Insert your Mac OS X install media (I used my 10.4 Tiger DVD).
Click Open Apple Install Packages.
Select your preferred installation media from the list (probably just one entry) and press the OK button. This will take a some time as it reads lots of package information from the CD/DVD.
Click the Find toolbar button, type Script Editor Scripts in the find window and press enter.
Click on Script Editor Scripts in the search results drawer.
Click the Install toolbar button.
(You are on your own from here, I do not know what other steps might be needed. I did not do the install, since I would normally just use pax for this.)
If your Mac OS X install media is close enough to mine, you can skip the long delay in Open Apple Install Packages and just open the one you will need (Essentials.pkg):
Run Pacifist.
Insert your Mac OS X install media.
Click the Open Package. button.
Navigate to the System/Installation/Packages directory of your install media.
Select Essentials.pkg and click the Open button.
(same as above, starting with Find)
If you end up using Pacifist, you should register it. It seems like a very nice program.
The file Comment Tags.scpt is contained on my Mac in the folder “Script Editor ScriptsAddress Book”. Did you just leave out the Address Book part of the name or am I looking in the wrong place? Anyhow when I run the script you suggested I get an error message that I have to select some text before running the script. What text?
I have not tried the other suggestion about using some third-party program but will if necessary.
Sorry, I just realized that the suggested script was just a copy of the Comment Tags script. It runs fine if I select the lines to comment out. I suspect there was never anything wrong with my copy of Comment Tags (I never actually looked at it). In any event, running this did not restore the “Comment Tags” item in the context menu. Do I have any alternative to complete re-installation of the Mac OS?
Ok from scratch, from what I understand when in Script Editor, your context menu is missing an item or items.
One being Comment Tags
What ever should appear in the context menu in Script Editor including the Comment Tags ( script)
is stored in /Library/Scripts/Script Editor Scripts/
I am not sure why you are finding the Comment Tags ( script) else where. But I suspect you have accidentally moved them.
First, can you give me the full path (example: /Library/Scripts/Script Editor Scripts/) where you have found the script, and also what else is in the folder.
OK, the applescript scripts are all in one folder on my HD, they came that way when I purchased the iMac a few months ago and I have not moved them or copied them elsewhere. The path to the folder is Macintosh HD: Library: Scripts. Inside this folder are 17 folders (e.g., Address Book ScriptsAddress Book, Script Editor ScriptsAddress Book, etc). Inside the last folder I listed is two bare scripts (and 10 folders containing various scripts). The two bare scripts are named About These Scripts.scpt and Comment Tags.scpt. I want to emphasize that a search of my HD for Comment Tags yields only this file. By the way, maybe apple has just changed naming conventions for applescript files, but almost all of the folders inside the Scripts folder have the words Address Book as part of their name (e.g., Sherlock ScriptsAddress Book, etc.). There is no folder named “Script Editor Scripts.”
Ah, I should have spotted this from your earlier post.
It looks like for some reason, all the Folders in the Macintosh HD: Library: Scripts folder have had the words Address Book added to the end. Why I do not know. But it is not standard and not how a new Mac would come.
Rename all the folders in Macintosh HD: Library: Scripts by removing the Address Book part at the end.
Example:
From: Address Book ScriptsAddress Book to: Address Book Scripts
From: Script Editor ScriptsAddress Book to: Script Editor Scripts
When you reopen Script Editor every thing should be ok. I assuming no folders in side the above folders has Address Book
on the end.
Yes, it worked! Thank you very much. It shows how potentially dangerous scripting can be. I don’t know how it happened, but I suspect it was while I was attempting to run either applescripts or QuicKeys scripts.