This script is a subroutine of a bigger script that I did a couple of weeks ago which Rsync some folder than check for mhl verification. No, this version is the light version as it only verify mhl of an already existing folder and then send email notification on error and success verification. I’m currently using shell script command so that applescript wait for all mhl to be verify before sending the success email. The problem is that shell script does<t give any info on what it<s reining so we don’t know which mhl have already pass the verification.
set sourceFolder to POSIX path of (choose folder with prompt "Choose a folder to verify:")
property defaultGroups : {"Family", "Friends"}
property defaultLabels : {}
set theFiles to paragraphs of (do shell script "/usr/bin/find " & quoted form of (sourceFolder) & " -type f -name '*.mhl'")
tell application "Terminal"
if not (exists window 1) then reopen
activate
end tell
set Errorstate to 1
repeat with aFile in theFiles
try
do shell script "mhl verify -f " & quoted form of aFile
on error errmsg number errornumber
set y to 1 + errornumber
set Errorstate to 2
##Send the Message for error
set theErrorSubject to "error " & quoted form of aFile
set theErrorContent to errmsg
tell application "Mail"
##Create the message
set theErrorMessage to make new outgoing message with properties {subject:theErrorSubject, content:theErrorContent, visible:true}
##Set a recipient
tell theErrorMessage
make new to recipient with properties {address:"Tech@visionglobale.com"}
##Send the Message
send
end tell
end tell
end try
end repeat
if (Errorstate is equal to 1) then
##Send the Message for success
set theSubject to "Succeed " & sourceFolder
set theContent to "MHL verification passed successfully"
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {address:"Tech@visionglobale.com"}
##Send the Message
send
end tell
end tell
end if
Now, the easy way to go around this problem will be to go with a do script (as shown bellow), this way it open one terminal window by mhl and we can monitor the verification this way. The only problem with this is that appplescript doesn’t wait for all of my terminal process to finish beforee jumping to the other command. My question is, is there a way to wait for all terminal windows to finish their process before going to the next command?
ell application "Terminal"
do script "mhl verify -f " & quoted form of filePath
Here’s an example that show how to run processes in sequence or parallel:
-- semicolon delimited list runs processes in sequence
-- and runs in new subshell
set subshell1 to "(result1='Message1'; sleep 10; osascript -e 'beep 1'; echo '<a>'$result1'<b>')"
set subshell2 to "(result2='Message2'; sleep 5; osascript -e 'beep 2'; echo '<c>'$result2'<d>')"
set cmd to subshell1 & " & " & subshell2 -- '&' runs processes in parallel
do shell script cmd
thanks for the reply, though I’m not quite sure that I get it. this will make my shell script runs in sequence? Isn it why shell script behave already? Applescript is already waiting for shell script to complete before running the next command. the issue that I’m having with shell script is that it doesn’t give any output as where it is in the process so we don’t really know what’s going on until the shell script is complete.
That’s the reason why I was trying to run script instead of a shell script. Script open one terminal window for each process and we can monitor their process as it runs (output is shown in terminal windows). But the script method isn’t running in a sequence, Applescript jump to the need command before all of terminal windows finished ruining
.
I have been trying to learn how to use the cocoa NSTask to run a shell script and get informed when the shell script is done. I think there is a unix way, but here’s what I have so far. Here is the unix script which I place on the desktop and make executable:
Here is the Library Script:
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSSpeechSynthesizer
property theSender : missing value
property theTask : missing value
on runScript:thePath sender:sender
set my theSender to sender
set theTask to current application's NSTask's launchedTaskWithLaunchPath:(thePath) arguments:{}
theTask's waitUntilExit()
tell application "System Events"
display dialog "I'm done."
end tell
end runScript:sender:
Here is the script that calls the library script that runs the shell script and waits until it is done:
use theScript : script "RunShellScript"
use scripting additions
theScript's runScript:("/Users/kelhome/Desktop/Hello.sh") sender:me
I couldn’t figure out how to get a notification. But, I was thinking that you could use a NSTimer to constantly query the task to check on its status. Something like that. Eyes are getting dim.
If you didn’t find anything else yet, I had time to work on this example for the library script:
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSSpeechSynthesizer
property theSender : missing value
property theTask : missing value
on runScript:thePath sender:sender
set my theSender to sender
set theTask to current application's NSTask's launchedTaskWithLaunchPath:(thePath) arguments:{}
repeat with i from 1 to 10
set r to (theTask's isRunning) as boolean
if r then
say i
delay 1
else
exit repeat
end if
end repeat
theTask's waitUntilExit() -- to just wait
tell application "System Events"
display dialog "I'm done."
end tell
return r
end runScript:sender:
The repeat loop is finite for testing reasons but you can change that.