Running sub-script without waiting for it to complete.

>>> So far this is what I have and is working <<<<


-----------------------------------------------------------

--- Calls up and runs a saved scpt file ----

-----------------------------------------------------------

tell application "Finder"

display dialog "I'm going to run a saved applescript"


delay 1

run script file "Mac Pro SSD:Users:Michael_Garito:Desktop:Test:Saved_Applescritp_Test.scpt"

display dialog "I ran a saved applescript"

end tell

-----------------------------------------------------------

This code shows the first dialog, after clicking 'ok', it calls up the script bellow and runs runs it as expected, then promprts the second dialog just as expected.


-----------------------------------------------------------

---- Saved_Applescritp_Test.scpt ------

-----------------------------------------------------------

tell application "Finder"

try

set theFilePath to (path todocuments folderfromuser domain) as text

repeat with I from 1 to 5

set theFileName to ("test" & I & ".txt")


makenewfileattheFilePathwith properties {name:theFileName}

delay 1

end repeat

on error


-- do nothing --

end try

end tell

-----------------------------------------------------------

This short script just makes five empty text files in the users (my) documents folder.

It works flawlessly.



>>>>> What I'm trying to accomplish and where I'm completely stuck... <<<<<<<


-----------------------------------------------------------

--- Calls up and runs a saved scpt file ----

-----------------------------------------------------------

tell application "Finder"

display dialog "I'm going to run a saved applescript"


delay 1

ignoring application responses

run script file "Mac Pro SSD:Users:Michael_Garito:Desktop:Test:Saved_Applescritp_Test.scpt"

end ignoring

display dialog "I ran a saved applescript"

end tell

-----------------------------------------------------------

This code shows the first dialog, after clicking 'ok', then displays the second dialog, wihout actaully runnign the test script.


What I'd like to happen is that the script is called up and ran in the back ground allowing me to move forward wihtout waiting on it to return as the result has no baring on the rest of the primary script.

Mac Pro (Early 2008), OS X Mountain Lion (10.8.2), The 8 cored beast...

Posted on Feb 23, 2013 3:47 PM

Reply
7 replies

Feb 23, 2013 5:22 PM in response to MGarito

Hello


You may use "do shell script" to execute osascript(1) in background. Something like this:


--set scptf to ("Mac Pro SSD:Users:Michael_Garito:Desktop:Test:Saved_Applescritp_Test.scpt" as alias)'s POSIX path
set scptf to "/Users/Michael_Garito/Desktop/Test/Saved_Applescritp_Test.scpt"

display dialog "I'm going to run a saved applescript"
delay 1
do shell script "/usr/bin/osascript " & scptf & " &>/dev/null &"
display dialog "I ran a saved applescript"


Or if you want to know any errors from osascript, use somthing like this instead:


do shell script "/usr/bin/osascript " & scptf & " &>~/desktop/log.txt &"


which will write STDOUT and STDERR from osascript to log.txt in desktop. Note that log.txt is created whether or not error occurs. Also osascript writes the result of the last statement in script to STDOUT. Thus this log.txt will contains script's result and/or error text.


Regards,

H

Feb 23, 2013 8:12 PM in response to red_menace

These are mostly simple task but that are going to be delayed heavily and repeated heavily, but the data collected is not needed for the remainder fo the application to proceed with loading / running.


For Example:

Get %CPU used, delay 5 minutes, save value to file, repeat until application closed.


Then later in the application, if desired I could pull data from the saved values from any of the many diffrent scripts saving values simultatiously.


I've taken Hiroto'snote and merged it into the exaple of what i'm trying to achieve that I tossed together earlier and came up with this, which is fully functional.


set listOfScripts to ["Testscript1.scpt", "Testscript2.scpt", "Testscript3.scpt", "Testscript4.scpt"]

repeat with I from 1 to number of items in listOfScripts

set theScriptToRun to itemI of listOfScripts

set scriptFile to ("/Users/Michael_Garito/Desktop/Test/" & theScriptToRun)

do shell script "/usr/bin/osascript " & scriptFile & " &>/dev/null &"

end repeat

display dialog "multiple scripts should be running"

return "done"


While each of the four testScript files runs simultatiously and dosen't delay the dialog from prompting or return from posting.


Thank you both for your assitance, it is greatly appreciated. 🙂

Feb 23, 2013 7:47 PM in response to MGarito

You could use NSTask to run the scripts via osascript like Hiroto mentioned - run script (and AppleScript in general) isn't really designed to run simultaneous scripts. What kind of tasks are you running that need to use an AppleScript in the background? An application doesn't take that long to load, and it would need to be loaded anyway before it could launch other tasks.

Feb 23, 2013 7:05 PM in response to red_menace

"There may be other approaches - what exactly is it you are wanting to do?"


So I'm workiugn towards running somthing like this from an applescript application I'm building in Xcode.


Set theListOfScriptsToRun as [ ScriptFile1.scpt, ScriptFile2.scpt, ScriptFile3.scpt, ScriptFile1.scpt, ...ect... ]


repeat with i from 1 to number of items in theListOfScriptsToRun

set scriptToRun to item i of theListOfScriptsToRun

ignoring application responses

run scritp scriptToRun

end ignoring

delay 1

end repeat


So while the application is loading, I can run a few dozen completley independent and unreleated scripts simultatiously.

Feb 23, 2013 8:25 PM in response to MGarito

Note that AppleScript in Xcode is more event driven and doesn't necessarily need repeat loops or delays, and any user interface (including menus) can be blocked if the script doesn't give the system any time to update things.


Simple scripts like the one mentioned could also just be placed into their own handlers, with repeating timers set up to run the handlers at the desired interval.

Feb 23, 2013 9:14 PM in response to MGarito

Just for the sake of indulging the obvious, the most straightforward solution to this problem is to save each of the four scripts as an application, and then run them from the main script using an 'ignore application responses' block:


set listOfApps to ["Testscript1.app", "Testscript2.app", "Testscript3.app", "Testscript4.app"]

repeat with thisApp in listOfApps

ignoring application responses

tell applicationthisApp to activate

end ignoring

end repeat

display dialog "multiple scripts should be running"

return "done"


If you saved the original app as a stay-open application, you could even use it to monitor the other running processes. Six-to-one, maybe, but...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Running sub-script without waiting for it to complete.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.