Script from a Script?

Now that I have my ical events being generated and deleted by Applescript I need to create a complied script in a specified sub directory of the current user then attach it to the event alarm. What would be the best way to go about that?

It’s easy to create script files via AppleScript using the ‘store script’ command:

script myscript
	display dialog "Boo!"
end script

set userDir to path to current user folder -- get current user folder
set scriptPath to userDir & "subdir:boo" -- append the subdirectory/path
store script myscript in file scriptPath -- save the script

Once saved it should be easy to attach it to an iCal alarm.

How do you specify the file name of the script file? What if I want to use a variable inside the Script myscript that is defined outside of that script?

That’s done by the line:

set scriptPath to userDir & "subdir:boo" -- append the subdirectory/path

In this case, the file name is ‘boo’ and the script is saved in the folder called ‘subdir’ inside the user’s home directory. You need to adjust this line to match the specific folder/file names you want to use.

Since the embedded script will be run dynamically, the easiest way to do that is via script properties:


-- define your base script
script myScript
   -- include properties for all variable data
   property messageToDisplay:""

   display dialog messageToDisplay
end script

-- now, in your main script you define the variables before you save the script:
set myScript's messageToDisplay to "Hello World!"

-- and here's the original code to save the script in a file
set userDir to path to current user folder -- get current user folder 
set scriptPath to userDir & "subdir:boo" -- append the subdirectory/path 
store script myscript in file scriptPath -- save the script

When run, the embedded script’s properties will be set and saved with the script when you ‘store script’. You can repeat this multiple times saving the same embedded script with different properties each time.

Here is my resulting code

The resulting script that is created looks like this:

The resulting script doesn’t seem to have the property set, properly. Also I should note that I had to specify scriptpath as string or it didn’t put it together right.