Script to change a Script (.scpt) in to an Application (.app)?

I have just upgraded my server from OS 10.6 to 10.12. In the calendar application of 10.12 it is no longer possible to run AppleScripts in Events. It is however possible to run Applications.

I have about 100 scripts (.scpt) that run regularly and are triggered by the Calendar Application.

Does anybody know of a quick way to change them to applications (.app)? Maybe a script to do so?

Model: MM
AppleScript: 2.9
Browser: Safari 605.1.15
Operating System: macOS 10.12

Script Editor has an Applescript dictionary with a “save” command that allows choosing to save as an application, so this should be easily scriptable.

Thanks t.spoon.

I am not a great scripter, do you know if there is one somewhere?

Where do the scripts live? Are they all together in one folder?

You may try :



set sourceFolder to choose folder

set p2D to path to desktop as text # Edit to fit your needs
set theDest to "applis" # Edit to fit your needs
set destFolder to p2D & theDest & ":"

tell application "System Events"
	if not (exists folder destFolder) then
		make new folder at folder p2D with properties {name:theDest}
	end if
	
	set theScripts to (disk items of sourceFolder whose name extension is "scpt")
	repeat with aScript in theScripts
		set destName to text 1 thru -6 of (get name of aScript) & ".app"
		set destPath to destFolder & destName
		my exportAsApplication(aScript, destPath)
	end repeat
end tell


on exportAsApplication(aFile, destPath)
	tell application "Finder"
		set aFile to aFile as alias
	end tell
	tell application "Script Editor"
		open aFile
		save document 1 as "application" in file destPath
		close document 1
	end tell
end exportAsApplication

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 29 octobre 2018 19:39:55

Morning Yvan, good to see you are still contributing after all those years and thanks for this one.

The script works in a way. It indeed converts .scpt to .app. However the .app does not run.

I tested with a simple one:

tell application "Disk Utility" to activate

Runs fine as a .scpt. Does not run once ‘translated’ by your script in to a .app.

If I make my own .app from it, it runs fine.

Any suggestions?

in several folders, but this can be rearranged if needed.

I tested Yvan’s script to see if the .app’s it generated ran here, and the one I tested also did not, run popped up an error message saying that it had been saved without being compiled and could not run.

This seems odd to me, because it was compiled and saved as a .scpt, and no changes were made once it was opened.

And you didn’t mention an error message, so maybe this isn’t what you’re experiencing. But for me, it worked to modify Yvan’t script to compile before saving.

set sourceFolder to choose folder

set p2D to path to desktop as text # Edit to fit your needs
set theDest to "applis" # Edit to fit your needs
set destFolder to p2D & theDest & ":"

tell application "System Events"
	if not (exists folder destFolder) then
		make new folder at folder p2D with properties {name:theDest}
	end if
	
	set theScripts to (disk items of sourceFolder whose name extension is "scpt")
	repeat with aScript in theScripts
		set destName to text 1 thru -6 of (get name of aScript) & ".app"
		set destPath to destFolder & destName
		my exportAsApplication(aScript, destPath)
	end repeat
end tell


on exportAsApplication(aFile, destPath)
	tell application "Finder"
		set aFile to aFile as alias
	end tell
	tell application "Script Editor"
		open aFile
		tell document 1
			compile
			save as "application" in file destPath
			close
		end tell
	end tell
end exportAsApplication

@ChangeAgent

I’m puzzled.
Here, when I use the script to convert your sample I get an application doing the job.

Which operating system are you using ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 30 octobre 2018 14:48:59

Hi t.spoon.

I do not get it to work with your script either. Same result the .app does not open stuff.

I tested the following, as .app and as .scpt, both created by me. Both opened the DU etc.

tell application "Disk Utility" to activate

Next I run the .scpt through the script and the resulting .app file does not open DU. That is where I am stuck at present.

PS also tried it with:

tell application "TextEdit" to activate
tell application "Terminal" to activate

and had the same result!

Mm I am puzzled too! I too would expect it to work. I tested it on two computers (both same OS) and get the same result.

AppleScript: 2.9

Operating System: macOS 10.12.6

me again.

I just had a thought. My scripts are old, some of them 2 or 4 or 6 or more years old. And work as are. But could it be that there is something in them or missing that makes them not work once run through your solution?

So I made a bunch of new Apple Scripts, (

tell application "Disk Utility" to activate

) and ran them through, and low and behold they work.

This could explain why they are working on your end and not on mine. Or?

Is there a way we can solve this? Like make a copy paste or something?

Love to hear and hope we can solve this.

Hi.

Another way to convert the scripts would be to use ASObjC and the (undocumented in Xcode) OSAKit framework, not opening the scripts in Script Editor, but I can’t tell if it would be any more successful with your scripts. In the script below, you have to choose the .scpt file(s) you want converted, but it can easily be convert to work with all such files in a folder:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "OSAKit"
use scripting additions

set selectedScptFiles to (choose file of type {"com.apple.applescript.script"} default location (path to scripts folder) with prompt "Select .scpt file(s) to resave as app(s) …" with multiple selections allowed)

repeat with thisFile in selectedScptFiles
	set sourceURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of thisFile))
	set thisScript to (current application's class "OSAScript"'s alloc()'s initWithContentsOfURL:(sourceURL) |error|:(missing value))
	set destinationURL to (sourceURL's URLByDeletingPathExtension()'s URLByAppendingPathExtension:("app"))
	tell thisScript to writeToURL:(destinationURL) ofType:(current application's OSAStorageApplicationType) usingStorageOptions:(current application's OSANull) |error|:(missing value)
end repeat

Hi Nigel,

Your script works too. But I have the same results as before. The .app are there, run, but do not open an Application as it should and does in scripted format.

Applications opening other applications. Could be a security issue, I suppose. Is there anything in System Preferences → Security & Privacy → Privacy → Automation on your machine to indicate that Calender has asked to control your script apps, or that they’ve asked to control other applications? If there is, you may be able to fix things there.

Just to be sure we’re all on the same page here, you’re saying that if you open one of these old scripts and manually run it from Script Editor, it works. And if you manually ave it as an application from Script Editor, then it works. But using the script to automate saving it from Script Editor, the old scripts, converted to apps, don’t work.

But new scripts do.

Is all of that correct? If so, I find that completely bewildering.

In that case be bewildered!

You are right indeed that is the case.

@Nigel

ChangeAgent wrote that he is running 10.12.6.
I’m not aware with such problem of permissions with this system.

@ChangeAgent

What if you open a script, copy it, paste in a new script and save the new one as an application ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 30 octobre 2018 18:34:05

Hi Yvan, thanks. Yes that is a way to go, would work, but a lot of extra work. But if it is that way it is that way…

I asked because if this scheme works, it may be coded to automate it.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 30 octobre 2018 20:48:15