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?
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
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
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?
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
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.