I wanted to save about a hundred applescript .scpt files as plain text .applescript. So I wrote the following code.
tell application "Finder"
set thefiles to selection as alias list
repeat with i from 1 to count of thefiles
set thefile to item i of thefiles
tell application "AppleScript Editor"
open thefile
-- code to save as applescript
tell application "System Events"
delay 0.5
tell application process "AppleScript Editor"
set frontmost to true
-- alternative method
--click menu item 9 of menu 1 of menu bar item "File" of menu bar 1
click menu item "Save As." of menu "File" of menu bar item "File" of menu bar 1
keystroke tab & tab & tab & tab & tab & tab & tab
key code 125 -- down key
key code 125
key code 125
key code 125
keystroke return
delay 0.2
keystroke return
keystroke "w" using command down -- close window
delay 0.5
end tell
end tell
end tell
--move thefile to trash -- use with caution, deletes file
end repeat
end tell
You really mustn’t use UI scripting when an application has a perfect suitable dictionary. This script is probably a lot faster:
tell application "Finder" to set myFiles to selection
set myFolder to (choose folder with prompt "Destination folder:")
repeat with i in myFiles
set i to i as alias
-- create dest alias
set destFile to useFilename_InFolder_withExtension_(i, myFolder, "applescript")
tell application "Script Editor"
set myDoc to open i
save myDoc as "text" in destFile
close myDoc
end tell
end repeat
on useFilename_InFolder_withExtension_(filename, foldername, ext)
set fName to lastPathComponent(POSIX path of filename)
set fExt to name extension of (info for filename)
set fName to text 1 thru -((count fExt) + 2) of fName
set fName to fName & "." & ext
set newFilename to (foldername as text) & fName
end useFilename_InFolder_withExtension_
(* ========= HANDLERS ========= *)
on pathComponents(filename)
if filename is "" then return {}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set components to every text item of filename
set AppleScript's text item delimiters to ""
if filename starts with "/" then set item 1 of components to "/"
-- if end with slash
if item -1 of components is "" then set components to (items 1 thru -2 of components)
set AppleScript's text item delimiters to tid
return components
end pathComponents
on lastPathComponent(filename)
set allComponents to my pathComponents(filename)
if allComponents is {} then return ""
return item -1 of allComponents
end lastPathComponent
You don’t even need to open/save the file with script editor… use the command line tool osadecompile. And if you need to recompile the “.applescript” files at some point use osacompile.
tell application "Finder" to set theSelection to selection as alias list
set destFolder to (choose folder with prompt "Where do you want to save the files?") as text
repeat with aFile in theSelection
set newPath to destFolder & getName(aFile) & ".applescript"
do shell script "/usr/bin/osadecompile " & quoted form of POSIX path of aFile & " > " & quoted form of POSIX path of newPath
end repeat
on getName(F)
set {name:Nm, name extension:Ex} to info for F without size
if Ex is not "" and Ex is not missing value then
set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
end if
return Nm
end getName
Here is a handler that decompiles and removes any nul-bytes.
Im not sure when this option were added, but I believe it came after Tiger.
You can use Smile to achieve the same for at least Tiger.
on getAppleScriptText(fileAlias) -- Thanks to oldmanegan
local theSourceText, qfPoxPath
set qfPoxPath to quoted form of POSIX path of (fileAlias)
set theSourceText to do shell script "/usr/bin/osadecompile " & qfPoxPath & "| /usr/bin/tr -d '\\000'"
on error e number n
tell me
activate
display alert "getAppleScriptText" & e & " : " & n
end tell
error number -128
end try
if theSourceText is "" then
tell me
activate
display alert "getAppleScriptText: The Script " & qfPoxPath & " is empty, or left in debugging state"
end tell
error number -128
end if
return theSourceText
end getAppleScriptText
I have created a tool based on this which displays the differences in two AppleScript files (script files ) via TextWrangler the Script can be found here