This is the tail end of a script. The last thing I want to do is create a new file with the contents of about 5 lines. It’s a plist file and in the shell I would just echo into the file. Here is the AS version:
set runScript to "echo <?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Enabled</key>
<integer>0</integer>
</dict>
</plist>
> ~/Library/Preferences/com.apple.iChat.AIM.plist; chmod 600 ~/Library/Preferences/com.apple.com.iChat.AIM.plist"
do shell script runScript
Of course, this doesn’t work. sh keeps balking on the newline. In the shell, I would enter a new line as , but even escaping that with \ in AS still gives me the following error:
sh: -c: line 2: syntax error near unexpected token <' sh: -c: line 2: ’
You can’t quote the entire thing, or the > redirector gets quoted, too. Example:
echo “this isn’t > going to do what you think it will.txt”
vs
echo “but this” > “will work.txt”
Additionally, never use shell commands without complete paths. (I also prefer to construct string more modularly, as you’ll see below.) Try something like this:
set theText to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Enabled</key>
<integer>0</integer>
</dict>
</plist>" as text
set myHome to (do shell script "/bin/echo $HOME")
set theFile to myHome & "/Library/Preferences/com.apple.iChat.AIM.plist"
try
do shell script "/bin/echo" & space & quoted form of theText & space & ">" & space & quoted form of theFile
-- broken into two lines for readability's sake
do shell script "/bin/chmod 600" & space & quoted form of theFile
on error theError
display alert "There was a problem creating or setting the permissions on \"" & theFile & "\"." as critical message theError
end try
set the_file to "~/Library/Preferences/com.apple.iChat.AIM.plist"
set the_content to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Enabled</key>
<integer>0</integer>
</dict>
</plist>"
set the_delim to ASCII character 10
tell (a reference to my text item delimiters)
set contents to ""
set {old_tid, contents} to {contents, the_delim}
set {the_content, contents} to {(the_content's paragraphs) as Unicode text, old_tid}
end tell
try
do shell script "rm " & the_file
end try
do shell script "echo " & quoted form of the_content & " > " & the_file & "; chmod 600 " & the_file
What’s with all the ‘do shell script’ stuff. No shell scripting is needed here. It’s all doable in plain vanilla AppleScript:
set theText to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Enabled</key>
<integer>0</integer>
</dict>
</plist>" as text
set myFile to (open for access file ((path to home folder as text) & "some.plist") with write permission)
set eof myFile to 0 -- clear out any existing contents
write theText to myFile
close access myFile