Scripting Applescript Editor

Hi,

I have an old script (back from when the current “Applescript Editor” used to be known as “Script Editor”) which would basically script Applescript Editor to create a new document, add a script to it, compile and save and quit. And it used to work…

Despite several trials and variations, the updated script doesn’t save anything. If I try to specify the file path in the properties of the newly created document, I get a “file doesn’t exist” error. If I skip the path in properties and try to specify it in the save line, using an alias errors ‘file doesn’t exist’ and specifying ‘file’ or nothing before the path does not work either. The save line requires that you specify “script” or “application” and I tried using “application” both with and without quotes. Also used the path with and without a file extension (the old version did not need a file extension to work, somehow)

Here’s the script:

tell application "AppleScript Editor"
	set myDocID to make new document at end of documents with properties {description:"A test of this", contents:"--test", name:"Applescript Scripting Test"} --path property could be used here but doesn't seem to work
	compile myDocID
	save myDocID as "application" in "OS X:Users:vangelon:desktop:Applescript Scripting Test.app" ”tried with or without file extension, with and without 'file' prefix, 'alias' fails
	-- save followed by string: "script","application", "script bundle","application bundle"
	quit saving no -- or "yes" or "ask"
end tell

What totally obvious thing am I missing?

Hey Vince,

Since I use Script Debugger I won’t bother taking this any further. After fiddling with things a bit I got the following script to work.

set _script to "tell application \"Finder\"
	set AppleScript's text item delimiters to \", \"
	set {x1,y1,x2,y2} to bounds of desktop's window
	display dialog \"Bounds of Desktop = {\" & result & \"}\"
end tell"

set _path to (path to desktop as text) & "GoofyScript.app"

tell application "AppleScript Editor"
	activate
	make new document at end of documents with properties {description:"A test of this", contents:_script}
	tell front document
		compile
		save as "application" in _path
	end tell
	quit
end tell

You’re confusing it by using the name property – it doesn’t seem to play well with autosaving. Keep it simple:

tell application "AppleScript Editor"
	make new document with properties {description:"A test of this", contents:"--test"}
	compile document 1
	save document 1 in file "Macintosh HD:Users:shane:Desktop:Whatever.app" as "application"
end tell

Thanks, guys. Those both seem to do the trick.

That’s interesting! But consider this variant:


set loggerPath to ((path to temporary items from user domain) as text) & "Test"
tell application "AppleScript Editor"
	set doc to make new document
	save document 1 in file loggerPath as "text"
end tell

tell doc
	set selection to insertion point -1
	set contents of selection to "-- New text"
end tell

This fails with “Can’t set last insertion point of document “Untitled 3” to last insertion point of document “Untitled 3”.” In fact, the title of the document is “Test”. How do I fix this?

Perfectly normal.

When you saved the file with the name “Test”

doc which is
→ document “Sans titre”

no longer point to your newly saved document which is :
→ document “Test”

Try this corrected script for see.


set loggerPath to ((path to temporary items from user domain) as text) & "Test"
tell application "AppleScript Editor"
	set doc to make new document
	save document 1 in file loggerPath as "text"
	
	set doc to document 1
	tell doc
		set selection to insertion point -1
		set contents of selection to "-- New text"
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) mardi 28 janvier 2014 15:28:56

Hello.

This wasn’t part of the OP’s original question. But I thought I’d just mention that the commandline command osacompile is much cheaper to use than AppleScript Editor, that is: you don’t have to have AppleScript Editor open. It is also no more difficult to pass along a string with a script than to pass it to AppleScript Editor.

See man osacompile in a Terminal window for further details.

@Yvan Thanks for the explanation. Thanks to the other posters, too. This thread has allowed me to get rid of an ugly workaround I was using in my ASUnit’s AppleScriptEditorLogger script, and hopefully fix the annoying “cannot autosave” message in Mavericks (but I need to test it more). For the curious, here is the commit with the relevant change: https://github.com/lifepillar/ASUnit/commit/c6db71d3ade4c0668185b40e36a623b18b28da33