Can an applescript run another applescript?

I have twenty individual Applescripts that I use by selecting them in my application’s (Quark) applescript pull down menu. This gets to be a bit cumbersome to navigate, so instead, I want to make a single script that pops up a dialog box that lists each script as a button so users can simply click which script to run.

I know that I could copy all of my scripts into one giant file and make the buttons work by using subroutines. :wink: BUT… I would like to keep my individual files separate and in tact. SO…

Is there a way to call and RUN another Applescript file?

Any insight in this would be appreciated,
Thanks

Hi,

of course it is

set myScript to alias ((path to scripts folder as text) & "testScript.scpt")
run script myScript

the example script testscript.scpt in ~/Library/Scripts must exist

Thanks Stefan. It works perfectly.:smiley:

I actually use this quite a bit. You can run subroutines that are in the loaded script and set variables in the loaded script. I have a Quark menu print script with variables for a number of the values, and then a drag and drop print script for batch printing that loads the menu script.


set myScript to load script file ((path to scripts folder as text) & "testScript.scpt")
tell myScript
	set it's localVariable to "This Value"
	subRoutine1() --subroutine in the loaded script, in case you don't want the whole script to run.
end tell

Here is my script i made for itunes

 display dialog "Which Version of ITunes applescript would you like to use?" buttons {"Version 4", "Version 3.4", "Version 1"}
set versionchoice to (button returned of result)
if versionchoice is "Version 4" then
	set Version_4 to alias ((path to desktop folder as text) & "Version 4.scpt")
	run script Version_4
else if versionchoice is "Version 3.4" then
	set Version_34 to alias ((path to desktop folder as text) & "Itunes Applescript Version 3.4.scpt")
	run script Versi_34
else if versionchoice is "Version 1" then
	set Version1 to alias ((path to desktop as text) & "Itunes Quick Open.scpt")
	run script Version1
end if
 

@chanced: I use that for QuarkXPress but I do that automatically…

@yeldarb: I use it in Quark with shortcuts (system preferences->keyboard). Now users use the undefined function keys in quark. They preffered that over a ‘launchpad’

I have another question on this topic.

Lets say you executed an external script, is there anyway to test if the script has fully completed and isn’t hung up? And then can you terminate the execution of the external script?

The mayor obstacle here you have inside AppleScript is that it is single threaded. This menas that when a handler is executing (plain applescript will be wrapped inside a run handler), all other handlers needs to wait before the previous handler is finished. So even if you found yourself a way sending multiple events to the application it is still queued.

Than the script itself isn’t really the problem in most cases. When a script seems to freeze it is in most cases the target application that’s the one that is not responding and your script is just waiting and doing nothing.

So using a timeout in AppleScript or a timeout outside AppleScript (with backgrounding) you still haven’t solved the problem in the target application.

Well the best approach for me would be using time outs outside AppleScript. Background a piece of applescript code and watch what is does. When the process is after a while is still alive it means it has frozen or at least something went wrong, so kill it.

thanks for the information. Really can help in the future. Thanks

I have a related question,I use this to run AppleScripts from other AppleScripts.

run script alias ((path to home folder as text) & "Dropbox:Changing Alias File")

I also use the following AppleScript to open any file and the alias can change (meaning I make a new alias delete the old one and give the new one the same name), it only cares about the file name. However I don’t want to open the script I want to run it.

tell application "Finder"
	activate
	open file "Changing Alias File" of folder "Dropbox" of folder "UserX" of folder "Users" of startup disk
end tell

Is there anyway to hybrid the two scripts so I can always run an AppleScript from an alias location and name?

I use quicKeys mapped to buttons and it is much easier to change the alias’ than all the QuicKeys mappings.

Hello Skillet.

I believe that you can somehow use a symbolic link. A symbolic link ln -s, (man ln) make for a file, that points to the name&path of another file. If you copy over a file to a name that is pointed to by the link, and then open the symbolic link you should be able to manipulate which applescript is run, without having to reassign any keyboard short cuts. :slight_smile:

Works like a charm, good call! I use SymLink app all the time but still more friction than selecting multiple files and pressing command+L for creating alias’.

Thanks for the push and idea, I just installed SymbolicLinker as a contextual menu in Mavericks and I can now select multiple files and create all the symbolic links quickly, no terminal, or choose source and destination, yay! http://www.macupdate.com/app/mac/10433/symboliclinker

Thanks McUsrII for the suggestion I’m in business:D

Greetings,

This thread has been all about running applescripts with applescript. So, is it possible to have one script run several open scripts, one after another?

In other words, I’ll open scripts A,B, and D. The run script will say, If script A is open, then run & close. If script B is open, then run & close. If script C is open, then run & close. If script D is open, then run & close.

The iTunes script above seems most promising template for this. What do you think?

Store the scripts as applets, run them and call the correct handler from them one by one. You still have the drawbacks I mentioned earlier.

Hi DJ Bazzie Wazzie,

I’m a newbie. Applets, handlers, usw. is all greek to me. But the drawbacks to which you refer are the scripts getting hung up, right?

How about a script that says “open script A, run, close. Open script B, run, close. Open script C, run, close.” In that way, there are but two scripts open and running at any given time. Possible?

Problem is that when one of the scripts hangs everything hangs. Normally when you build an script running other scripts you want to build in some control as well. Like when one of the scripts hangs the process should be stopped gently or the current script should be restarted. This is not simple to create in AppleScript.

Just use StefanK’s example in one of the early posts:

set theFile to choose file with prompt "choose script"
run script theFile

The script above will prompt you to choose a script and run it (if there is anything to run).

When you have multiple files and want a loop over an list of alias it can look like this:

set theScriptFiles to {aliasOfScriptA, aliasOfScriptB, aliasOfScriptC, aliasOfScriptD, aliasOfScriptC}
repeat with scriptFile in theScriptFiles
run script (contents of scriptFile)
end

NOTE: replace the variables AliasOfScriptA for real alias objects

I definitely want to run a list of scripts. But naturally, I get tangled on the small stuff:

set FAA to alias (("Desktop:current revs:scripts:FAA.scpt"as text) & "SXX_FAA.scpt")
set FBB to alias (("Desktop:current revs:scripts:FBB.scpt" as text) & "SXX_FBB.scpt")
set FCC to alias (("Desktop:current revs:scripts:FCC.scpt" as text) & "SXX_FCC.scpt")
set theScriptFiles to {aliasOfFAA, aliasOfFBB, aliasOfFCC}
repeat with scriptFile in theScriptFiles
	run script (contents of scriptFile)
end repeat

And I get the ol’ “The variable aliasOfFAA is not defined.” message. Typical for me!

And the paths must be full HFS paths or relative paths like

set FAA to alias ((path to desktop as text) & "current revs:scripts:SXX_FAA.scpt")

Then write

set theScriptFiles to {FAA, ... }

Hi Stefan & DJ___,

It works!

set FAA to alias (("...:Desktop:current revs:scripts:" as text) & "FAA.scpt")
set FBB to alias (("...:Desktop:current revs:scripts:" as text) & "FBB.scpt")
set theScriptFiles to [FAA, FBB]
repeat with scriptFile in theScriptFiles
	run script (contents of scriptFile)
end repeat

Since the FAA and FBB scripts work with Acrobat, there’s some issues to iron out – such as hang ups. I solved those by adding a “quit application” at the end of each of those scripts and inserting a delay of 2 seconds. That straightened out the error -609, “cannot communicate with application” message, and allowed this script to proceed to the next script in line.

So, that’s progress all around.

Thanks!

Hello Stefan. Max again,

I am trying to run a second script from inside the one I am running now. The script is running as a mail action

here is a fragment from the end of the script before I added the extra lines

                    set the read status of messages of mailbox "baggage archive" to true
	   end tell	
end perform mail action with messages	

end using terms from

As you see I am right at the end of the mail actions script.

Here is the fragment with my two lines added

                 set the read status of messages of mailbox "baggage archive" to true
	end tell	
           
           set TwoScript to alias (("Macintosh HD:Library:Scripts:Folder Action Scripts:" as rich text) & "image format change.scpt")
           run script TwoScript		

end perform mail action with messages	

end using terms from

When i try to compile i get “expected end of line but got TwoScript” in the second of the two new lines.

If put the same two lines

set TwoScript to alias ((“Macintosh HD:Library:Scripts:Folder Action Scripts:” as rich text) & “image format change.scpt”)
run script TwoScript

into a new empty script, it compiles and it works.

Is it not possible to run a second script from inside mail actions??

Thanks, Max