Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Scripting Applescript Editor and saving files

I've created an Applescript that generates a smaller, simpler script. I've gotten everything to work, but when I tell Applescript Editor to save the file, it doesn't work. It doesn't error out, it acts like it finished properly, but nothing shows up in the location I told it to save. Here's the snippet that's giving me problems:


tell application "AppleScript Editor"

activate

makenewdocument

set contents of document 1 to fnlScrpt

compiledocument 1

save document 1 in "Macintosh HD:" as "application"

closedocument 1 without saving

end tell


I'm going to define a more specific save location once I get it working, but for now it just saves to the top level. It needs to save as an application, and close the script without saving it. Any help is appreciated!

Posted on Aug 31, 2011 5:57 PM

Reply
Question marked as Best reply

Posted on Aug 31, 2011 6:58 PM

Okay, fixed it on my own, just so if anyone else has a similar problem you'll. Here's what it should look like:


tell application "AppleScript Editor"


activate


makenewdocument

set contents of document 1 to fnlScrpt


compiledocument 1

save document 1 as "application" in file ((path to desktop as Unicode text) & "Test.app")


closedocument 1 without saving

end tell


I'm sure there are other ways to do it as well, but that worked for me!

13 replies
Question marked as Best reply

Aug 31, 2011 6:58 PM in response to woz.was

Okay, fixed it on my own, just so if anyone else has a similar problem you'll. Here's what it should look like:


tell application "AppleScript Editor"


activate


makenewdocument

set contents of document 1 to fnlScrpt


compiledocument 1

save document 1 as "application" in file ((path to desktop as Unicode text) & "Test.app")


closedocument 1 without saving

end tell


I'm sure there are other ways to do it as well, but that worked for me!

Sep 1, 2011 2:35 PM in response to woz.was

I'm glad you found your original problem - you didn't specify a file name to save the file into (I'm even more glad it failed and didn't affect your hard drive since what you were asking it to do was replace the contents of your hard drive with the script... but I digress).


You're also going about this the long way - forcing Script Editor to open, create a new document and save it, whereas AppleScript can do this all on its own via the store script command:


script myScript


beep

end script


store scriptmyScriptin (path tostartup diskastext) & "Test.app"

Sep 2, 2011 6:22 AM in response to Camelot

Dude, thanks so much! That's much easier, and cut out 2 useless sections of my script, fantastic. It works perfectly now, and much faster. Here's the finished product if you're interested:


global shlScrpt


set shlScrpt to ""

set dne to false


repeat until dne is equal to true

set dsply to (display dialog "What's the UNIX newb?" default answer "~blah" buttons {"Cancel", "Add to Queue", "Done"})

set buttrtn to button returned of dsply

set rslt to text returned of dsply


if buttrtn is "Add to Queue" then

set shlScrpt to (shlScrpt & rslt & " && ")

else if buttrtn is "Done" then

set dne to true

if shlScrpt is "" then

set shlScrpt to (shlScrpt & rslt)

else

set shlScrpt to (shlScrpt & rslt)

end if


script myScript

tell application "Terminal"

activate

do script "cd /users/"

delay 1

do scriptshlScrptinwindowfrontmost

end tell

end script


store script myScript in (path to desktop as text) & "Test.app"


end if

end repeat

Sep 2, 2011 1:29 PM in response to woz.was

If you plan on running it by itself, your stored script won't do as you expect because it won't know what the shlScrpt variable is. I'm still wondering if you have made some global preferences changes (auto-save, versions, etc), since I've used a script snippet generator/code wrapper since at least Tiger, but it will no longer save in Lion.

Sep 2, 2011 6:22 PM in response to woz.was

Your "finished product" accidentally works because global variables are saved with the script. The variable shlScrpt is not actually defined anywhere in the saved script, so if something happens such as the script getting recompiled, the (inaccessible) global variables will be lost.


I use another approach - my script adds a wrapper script around the clipboard text or a selection in the AppleScript Editor so that it can be used later to insert code snippets or handlers into the current script document. I keep a library of these code insertion scripts in my Scripts Menu so that I can prototype a script fairly quickly. I don't normally auto-save these scripts since I add a description and a few comments before saving to the appropriate folder, but things are back to normal after discovering this new "feature".

Sep 7, 2011 8:09 AM in response to red_menace

Good call on the Accidentally Works, appreciate the feedback. I ended up writing a text file into the apps resource folder (using the "path to me" option) that contains the text i need, then using a cat shell script to call it up for the "shlScrpt" variable, so it stays with the app no matter where it goes. Thanks again!

Jan 12, 2012 5:11 PM in response to woz.was

Hi all, I'm a little late to this thread in an attempt to get some scripts working in Lion. Thanks for the help. I noticed the way you set your globals, and the way you're having variables get passed on to recompiled scripts.


I take a different approach, by writing my Applescript as text, in quotation marks, and then relying on fields getting filled in when the Applescript Editor is run, and the new script has the correct data. I make one text entry called "QT_multiliner" and fill in variables using quotation marks and ampersands.


THIS FIRST SCRIPT is used to take variables given to it by a database, and generate a custom applescript for any given record in the database.

----------------------------------------------------

set yyyymmdd to (((year of (current date)) * 10000) + ((month of (current date)) * 100) + (day of (current date))) as text

set mmdd to (text items 5 through 8 of (yyyymmdd)) as text

set qtPath to "/path/to/folder/" & mmdd & "/"

set RenderPrefix to "filename_of_choice"

set TGA_path to "/path/to/folder_with_files/" & mmdd & "/" & RenderPrefix & "/" as text

set appletPath to "/path/to/folder/applet_name_" & RenderPrefix & ".app"

set temp_doc to POSIX fileappletPath



--write the applet in Script Editor. Variables will be filled in and compiled in Script Editor.

set QT_multiliner to "tell application \"Finder\"

set thePath to \"" & TGA_path & "\" as string

activate

make new Finder window

set target of Finder window 1 to (POSIX file thePath)

set theSequence to get first file of the front window as alias

close window 1

end tell

set sequenceName to \"" & RenderPrefix & ".mov\"

tell application id \"com.apple.quicktimeplayer\"

activate

open image sequence theSequence frames per second 23.98

set savePath to \"" & qtPath & "/\" & sequenceName

tell movie 1

with timeout of 2400 seconds

export to (POSIX file savePath) as QuickTime movie using settings alias \"path:to:settings:file\"

end timeout

close saving no

end tell

quit

end tell"


-- Write the script to its own applet


tell application id "com.apple.ScriptEditor2"

set x to makenewdocument

set x to the front document

set the text of document 1 to QT_multiliner


compiledocument 1

save document 1 as "application" in (temp_doc)


quit

end tell

------------------------------------------------################################ ######


...now, when that is run, the following applescript is generated with all of the variables filled in.


------------------------------------------------################################ ######


--This applet was saved by the previous script, and is saved as /path/to/folder/applet_name_filename_of_choice.app


tell application "Finder"

set thePath to "/path/to/folder_with_files/0112/filename_of_choice/" as string


activate


makenewFinder window

set target of Finder window 1 to (POSIX file thePath)

set theSequence to get first file of the front window as alias


closewindow 1

end tell

set sequenceName to "filename_of_choice.mov"

tell application id "com.apple.quicktimeplayer"


activate


open image sequencetheSequenceframes per second 23.98

set savePath to "/path/to/folder/0112//" & sequenceName

tell document 1

with timeout of 2400 seconds

export to (POSIX file savePath) as QuickTime movie using settings alias "path:to:settings:file"

end timeout

closesavingno

end tell


quit

end tell

-------------------------------------------------------


I really liked Camelot's "script / end script" function, it's better, but I just couldn't get it to pipe variables through from one script to another. Since I need to run this script on any one of dozens of computers, I need the variables to be compiled and saved within each applet.


all the best,

Vic

Sep 18, 2013 4:58 AM in response to Victor_D

I've used this page for reference before (thanks everyone!) and I just wanted to add something which might help other people having a similar issue to the one I found myself with.


My Applescript created stand alone apps which would be passed a string (the path to the file to open) and, when run, would open the file using finder. This was to ensure that the correct Mac OS chosen software would launch when each type of file was opened (if MS Word was installed, Word would launch, if not, TextEdit would launch).


The script I used was very similar to Victor D's method, where a multiline text file is created using variables and then inserted into a new Applescript window and compiled/saved with a specific filename before closing the window and moving onto the next item (taken from a comma separated textfile of filenames).


Although this script worked on AppleScript Editor 2.2.1 on my old PPC G4, version 2.5.1 on my Intel Mac didn't work. It would keep hanging at random points, sometimes halfway through, sometimes further, but it never reached the end of the list to process. I put delays in the code of half seconds, thinking that the new Mac was processing stuff too fast and the script couldn't keep up, but it still kept hanging at random intervals.


Anyway - cut to the end of a long, boring story - the solution was that the original script-creating script was being run from within the Applescript editor itself - so it was trying to run and create new scripts simlutaneously, which confused it. By saving the original script-making script as a standalone app first and then running it as an app, the whole thing worked perfectly as it kept the two processes separate.

Scripting Applescript Editor and saving files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.