Re: Have files, want one script for each; how to automate creation of these scripts?
Re: Have files, want one script for each; how to automate creation of these scripts?
- Subject: Re: Have files, want one script for each; how to automate creation of these scripts?
- From: has <email@hidden>
- Date: Sat, 16 Mar 2002 01:06:38 +0000
Bill Christens-Barry wrote:
>
I have some files, say all of the files in a specified folder. I'd
>
like to create a set of scripts, one script per file, and save each
>
as a Classic apple. A typical script would go something like this:
>
>
tell app "targetApp"
>
activate
>
open "<the file that is unique to this script>"
>
end tell
>
>
How can I automate this (for 8.6 - 9.x) given that ScriptEditor is
>
not scriptable (at least not v1.4.3 that I'm using on my 8.6
>
machine)? Is there an alternative tool (perhaps a scriptable script
>
editor) that allows this?
Read up on properties. Great things; once you put a value into one, it
stays there forever (unless you change it or recompile the script). The
following handler creates script objects on demand: simply pass in a file
alias and it'll return you a script object with that value safely stored
within:
on makeNewScriptObject(fileAlias)
script
property _fileAlias : fileAlias
tell app "targetApp"
activate
open _fileAlias
end tell
end script
end makeNewScriptObject
-------TEST CODE-------
set aliasToMyFile to choose file with prompt "Pick the file for this script:"
set scriptObject to makeNewScriptObject(aliasToMyFile)
--> <<script>>
Next step is to save that script object as a script file on disk, which is
dead easy. Simply use the 'store script' in Standard Additions:
on saveScriptToDisk(theScriptObject, saveAsFileReference)
store script theScriptObject in saveAsFileReference
end saveScriptToDisk
-------TEST CODE-------
set storeScriptAsFile to new file "Save script as:"
saveScriptToDisk(scriptObject, storeScriptAsFile)
--> saved script file on disk
The last thing you need to remember is that scripts stored this way are
saved as regular files, not applets. But not to worry. An applet is simply
an AS script object stored in a sort of executable shell (i.e. an extra
program bit whose job is to sit around importantly appearing to the user as
an application. When run, it simply feeds the stored script to AS to run
and sits back and relaxes while AS does the real work).
So save an empty script to disk as an applet. Every time you wish to store
a script object, use the Finder to duplicate this applet.
on makeNewAppletShell(pathToApplet, nameOfCopy)
tell application "Finder"
duplicate pathToApplet to nameOfCopy
end tell
end makeNewAppletShell
So what you should end up with is a script containing the three above
handlers [i.e. makeNewScriptObject(), makeNewScriptObject() and
makeNewAppletShell()] along with the following code to make it all go:
property pathToApplet : alias "<path to empty applet here>"
choose file with prompt "Pick the file that is unique to this script:"
set scriptObject to makeNewScriptObject(result)
set storeScriptAs to new file "Save script as:"
makeNewAppletShell(pathToApplet, storeScriptAs)
saveScriptToDisk(scriptObject, storeScriptAs)
The only other thing I'd mention is that you can't open, edit and resave
your new applets in Script Editor; if you do that then the value stored in
the _fileAlias property will be lost and the script won't work any longer.
I find it often helps to add a small comment - something like:
on makeNewScriptObject(fileAlias)
script
(* This file generated by Blah-Blah script. DO NOT EDIT DIRECTLY! *)
property _fileAlias : fileAlias
tell app "targetApp"
activate
open _fileAlias
end tell
end script
end makeNewScriptObject
In practice you'll probably want to automate the file naming bit too,
rather than using dialog prompts to to get values for 'aliasToMyFile' and
'storeScriptAsFile'. But the above code should get you through the
stickiest stuff and I'm sure you can manage the rest.
HTH
has
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.