Here’s the new script. The only thing I have left to do is make it possible for the script to find the user.js file on any computer, not just my own. I’ve got a seperate topic up about this so hopefully someone helps me out.
(*Please note that this is a GUI Script, so while it is running, you will not want to do anything else.*)
--Get Answer
display dialog "Show pictures in Camino (Yes or No):" default answer "No"
copy the result as list to {Answer}
tell application "Finder"
--Check the existance of file "user.js" and if it does, to change the extention to .txt
if the file "The Cave:Users:chrisowen:Library:Application Support:Camino:user.js" exists then
set the name of the file "The Cave:Users:chrisowen:Library:Application Support:Camino:user.js" to "user.txt"
set Have to 1
else
try
--Try to make user.txt if user.js doesn't exist
make new folder at "The Cave:Users:chrisowen:Library:Application Support:Camino:" with properties {name:"user.txt"}
set Have to 0
end try
end if
end tell
--Open user.txt
tell application "TextEdit"
open file "The Cave:Users:chrisowen:Library:Application Support:Camino:user.txt"
end tell
--Bring TextEdit to the front
activate application "TextEdit"
--GUI Script TextEdit to change the line of javascript so pictures are off
if Answer is "No" then
tell application "System Events"
tell process "TextEdit"
--This is the command for "Find"
keystroke "f" using command down
repeat until exists window 1
delay 0.5
end repeat
--Window 1 is the "Find" window
if Have is 1 then
try
tell window 1
keystroke "user_pref(\"permissions.default.image\", 1);"
keystroke tab
keystroke "user_pref(\"permissions.default.image\", 2);"
delay 1.5
click button "Replace All"
end tell
end try
else
--Here's where people who didn't have a user.js file come in.
try
tell window 1
keystroke ""
keystroke tab
keystroke "user_pref(\"permissions.default.image\", 2);"
delay 1.5
click button "Replace"
end tell
end try
end if
delay 1
keystroke "s" using command down
end tell
end tell
delay 1
tell application "TextEdit"
quit
end tell
tell application "Finder"
set the name of the file "The Cave:Users:chrisowen:Library:Application Support:Camino:user.txt" to "user.js"
end tell
end if
--GUI Script TextEdit to change the line of javascript so pictures are on
if Answer is "Yes" then
tell application "System Events"
tell process "TextEdit"
--This is the command for "Find"
keystroke "f" using command down
repeat until exists window 1
delay 0.5
end repeat
--Window 1 is the "Find" window
if Have is 1 then
try
tell window 1
keystroke "user_pref(\"permissions.default.image\", 2);"
keystroke tab
keystroke "user_pref(\"permissions.default.image\", 1);"
delay 1.5
click button "Replace All"
end tell
end try
else
--Here's where people who didn't have a user.js file come in.
try
tell window 1
keystroke ""
keystroke tab
keystroke "user_pref(\"permissions.default.image\", 1);"
delay 1.5
click button "Replace"
end tell
end try
end if
delay 1
keystroke "s" using command down
end tell
end tell
delay 1
tell application "TextEdit"
quit
end tell
tell application "Finder"
set the name of the file "The Cave:Users:chrisowen:Library:Application Support:Camino:user.txt" to "user.js"
end tell
end if
I’m not using Camino so I can’t really help but I do have a couple tips for you…
You have to “escape” the characters which give you problems with a "" character… like so…
set aQuote to "\""
To make your code more portable to other people you can reference certain folders in MaxOSX instead of explicitly listing their path. There’s many of these folders but here’s how to reference the one you’re using…
set caminoFolder to ((path to application support folder from user domain) as string) & "Camino:"
Here’s another tip about gui scripting. Sometimes I find it easier to use “keystrokes” rather than explicitly using gui scripting commands. For example, where you are accessing the “find” command in TextEdit, since it has the keystroke shortcut of cmd-f you can use that instead. So where you have…
click menu item “Find.” of menu “Find” of menu item “Find” of menu “Edit” of menu bar 1
can be changed to:
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
keystroke "f" using command down
end tell
end tell
You can also move your cursor around in the Find window by using…
keystroke tab.
And to insert a text value into a field where your cursor is currently located you can use…
keystroke “insert your text here”
And to press the “OK” button, if it is the default button you cn use…
keystroke return
I’m not saying you want to use keystrokes, but sometimes it’s easier so use them when you can. Note though that the application where you are using the keystrokes has to be the frontmost application at the time you issue the commands. System events issues the keystrokes to the fronmost application, even if you say tell process “TextEdit”.
Since you want a text document (not RTF), I’d do this:
set someText to "Here is \"My\" new text"
set Loc to ((path to desktop folder as text) & "myNewDoc.txt")
set New to open for access Loc with write permission
try
write someText to New
close access New
on error
close access New
end try
tell application "TextEdit" to open Loc
Thanks you two. I believe the information you’ve given me will let me finish this script.
As far as the
click menu item “Find.” of menu “Find” of menu item “Find” of menu “Edit” of menu bar 1
goes regulus6633, I got the right context of that off Apple’s sample GUI script, “Set Network Location” It’s the right code and makes the “Find” window be frontmost, but the set value of text field 1 to “, 2);” part wasn’t working so I’ll try out the keystroke stuff.
And Adam Bell, I do believe you killed two bird with one stone there for me. You showed me how to use quotes inside of quotes and how to add text to a text file. I am very gracious! Thanks a bunch!