Run applescript from another sript

Hi,

I’m an applescript newbie and I’m setting up some home automation executing applescripts from a keyspan infrared remote. I now have too many scripts to allocate to the keyspan so I want execute a script to show all the scripts in a particular folder and then I can select and execute one of them.

I’ve made it list the files but I can’t see how to actually make the applescript that is selected actually run.

It must be obvious but…

tell application "Finder"
	set listoffiles to name of every item of folder "Macintosh HD:Automation"
	set scripttorun to choose from list listoffiles with prompt "Choose a function"
WHAT GOES HERE?
end tell

Hope you can help,

Many thanks,

Roland

Hi Roland,

you can do it like this:

set theFolder to "Macintosh HD:Automation:"
tell application "Finder" to set scripttorun to choose from list (get name of every file of folder theFolder) with prompt "Choose a function"
if scripttorun is false then return
run script (theFolder & item 1 of scripttorun) as alias

Oops - SK beat me to it… :confused:


set myFolder to "Macintosh HD:Automation" as alias
tell application "Finder"
	set listoffiles to name of every file of myFolder
	set scripttorun to (choose from list listoffiles with prompt "Choose a function")
end tell
run script ((myFolder as text) & scripttorun) as alias

FWIW, you can do it without the Finder:

set scriptFolder to "Macintosh HD:Automation:"
list folder alias result without invisibles
choose from list result with prompt "Choose a function"
try
	run script alias (scriptFolder & result's first item)
end try

Thanks so much, works first time!

Now its just a simple matter of programming 4 buttons on the Keyspan remote and I can run any script I want.

Roland

As I said it runs well - all three solutions, but it would look better not to show the file extension.

I know about the extension hidden command but where do you put it in the script?

Thanks for your help and patience,

Roland

There isn’t. Although I don’t have time to integrate it into what you’ve got so far, this finds the base name and extension of a file with an alias to the file in place of choose file:

set f to (choose file) -- for illustration
tell (info for f) to set {Nm, Ex} to {name, name extension}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
BN

The problem then becomes that to launch a script given the base name only, you have to put the name back together in full with the correct extension, and reconstruct its path. If all of your list have extensions of .scpt, that’s easy, and then a one-liner will do:

tell (info for (choose file)) to set basename to name's text 1 thru ((my (offset of ("." & name extension) in name)) - 1)

Although less compact than Adams oneliner extractor, this will do the job if you have no duplicate names in that folder:


set scriptFolder to "Macintosh HD:Automation:"
set tt to list folder alias result without invisibles
set r to {}
repeat with t in tt
	set tname to t as string
	set t2 to ""
	repeat with h in tname 
		if "." is in h then exit repeat
		set t2 to (t2 & h) as string
	end repeat
	set r to r & {t2}
end repeat
set x to choose from list r with prompt "Choose a function"
if x is false then return
tell application "Finder" to set gg to every item in folder scriptFolder whose name contains x
run script alias (gg as string)


Thanks so much, both solutions work wonderfully.

Roland

Hi Adam,

I use this strip-base-name-routine quite often, so I looked for a faster solution,
which also works properly if there is no extension.
This is approx. 25% faster (tested with Nigel’s timeLotsa routine):

tell (info for f) to set {Nm, Ex} to {name, name extension}
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm

Not so here.
To check for fast execution, I compared the two routines to derive the short names (moderately big scripts folder of 50 items):

Method 1 (rudimentary)


set scriptFolder to "Macintosh HD:Automation:"
set tt to list folder alias result without invisibles
set r to {}
repeat with t in tt
	set tname to t as string
	set t2 to ""
	repeat with h in tname --from 1 to count of characters 
		if "." is in h then exit repeat
		set t2 to (t2 & h) as string
	end repeat
	set r to r & {t2}
end repeat
set x to choose from list r with prompt "Choose a script"
if x is false then return
tell application "Finder" to set gg to every item in folder scriptFolder whose name contains x

run script alias (gg as string)

Method 2 (sophisticated: “strip-base-name-routine”)


set scriptFolder to "Macintosh HD:Automation:"
tell application "Finder" to set tt to every file in alias result

set r to {}
repeat with t in tt
	tell (info for alias (t as string)) to set {Nm, Ex} to {name, name extension}
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex's characters) - 1) of Nm
	set r to r & Nm --{t2}
end repeat
set x to choose from list r with prompt "Choose a script"
if x is false then return
tell application "Finder" to set gg to every item in folder scriptFolder whose name contains x

run script alias (gg as string)

Strange enough, the strip-base-name-routine DOES lose here…