Suppose you had 10 scripts in a group and 10 groups. There are so many ways to use on e set of 10 scripts, but what is the best? Some commands I can think of using are ‘load script’, run script’, do shell script “osascript …”, etc… Also there’s running it from a library. I don’t know if it would be a good idea to load all the scripts at one time. Suppose there are a thousand scripts you need to load.
I can’t see your workflow but what I’ve done in the past creating AppleScript code on the fly. The code was stored in a MySQL database and with one single query I could get the right AppleScript code to compile and run later. Of course you don’t need the database system I’ve used but a similar solution with osascript, grep and cat could be used as well (make sure the plain applescript text files contains a trailing newline)
to execute them:
do shell script "cat /path/to/scripts/*.applescript | osascript"
you can change the regex or use egrep to fine tune the selection of scripts you want to run.
Storing them as text works fine, but you do need a fairly controlled system. For example, if you save as text from Mavericks, the new terminology can result in text files that won’t compile in previous versions. You can also have problems if scripting additions vary. If you’re not using a database, I’d be inclined to run compiled scripts. Even if you are, I’d be inclined to have the database store the text and a path to the compiled form.
Of course my suggestion has maybe some limitations, however the reason I used an database was:
I can run OS specific code
I can run user specific code
I can run date specific code
I can run application (version) specific code
I can do login (user) tasks
I can do boot (system) tasks
I can update my code in the database and all machines will run the updated code the next run
You don’t need to install any kind of script on the machine (only the loader)
The reason I wrote this was mainly because it reduced maintenance costs almost to zero. When someone logs in on another workstation, it still runs the right versions of scripts. Also it was quite useful for the It-guys because they could write an Installer in AppleScript, enable it and reboot all workstations :). Originally I wrote this for one client but it’s more and more used in mean time.
Note: none of the machines (are allowed to) use any kind of scripting addition besides the standard scripting additions. It was also taken in consider when booting the work stations (automatically removed).
I just had an idea. What if you load it as text from the database or whatever. Then, you save as scptd with osacompile to the library. The only problem is how to change the bundle to cocoa-applescipt. Then, you can have access to the scripts by name. Each script would be a group of functions or subroutines.
Does anyone know how to change the bundle to cocoa-applescript?
I was looking at the info.plist for the compiled script bundle. There’s this:
It can’t be as easy as adding this to the info.plist. Just thought of this. When you osacompile the script as scptd, it won’t compile if there’s ObjC. Haven’t tried it yet but don’t think it will compile.
Got it working. Here’s an example that compiles the script from text and enables ASObjC in the info.plist:
set testScript to "use scripting additions
on Testing(t)
display dialog t
end Testing"
-- make scipt file specification
set scriptPath to quoted form of (POSIX path of ((path to library folder from user domain as string) & "Script Libraries:Tester1.scptd"))
-- make the script
do shell script "echo '" & testScript & "' | osacompile -o " & scriptPath
-- write to info.plist
do shell script "defaults write " & scriptPath & "/Contents/info.plist \"OSAAppleScriptObjCEnabled\" -bool 'yes'"
(* call the script with:
use theScript : script "Tester1"
--
theScript's Testing("hello")
*)
I still need to play around with it and add some error checking.
Made a mistake. Should have used ‘quoted form’ on the string instead of directly quoting the string. Probably, to escape the single quotes. So got it working now with ASOC framework:
set testScript to quoted form of "use AppleScript version \"2.3\"
use scripting additions
use framework \"Foundation\"
use framework \"AppKit\"
on playSound:theSound
set aSound to current application's NSSound's soundNamed:theSound
aSound's setVolume:(1.0)
aSound's play()
return ((aSound's |name|()) as text)
end playSound:"
-- make scipt file specification
set scriptPath to quoted form of (POSIX path of ((path to library folder from user domain as string) & "Script Libraries:Tester1.scptd"))
-- make the script
do shell script "echo " & testScript & " | osacompile -o " & scriptPath
-- write to info.plist
do shell script "defaults write " & scriptPath & "/Contents/info.plist \"OSAAppleScriptObjCEnabled\" -bool 'yes'"
(* call the script with:
use theScript : script "Tester1"
--
theScript's playSound:("Tink")
*)
Edited: thought I had the sound volume working. Now it’s not working. Need to look into that later.
Edited: disregard about the volume. It’s working, but doesn’t effect the system volume.