PDA

View Full Version : help with applescript




nicoisconfusion
Mar 14, 2012, 10:39 PM
Hi guys i have a script finished but having trouble with looping something.

The script needs to mount an external hard drive
wait until a process is no longer running then unmount the disk

this is what i have so far using so far.



set myVolumeLabel to "disk1s2"

on ApplicationIsRunning(BreakTime)
tell application "System Events" to set appNameIsRunning to exists (processes where name is BreakTime)
return appNameIsRunning
end ApplicationIsRunning


tell application "Finder"
set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[0-9]*' "
if not (disk myVolumeLabel exists) then
do shell script "diskutil mountDisk " & diskDev
end if
end tell


if ApplicationIsRunning("BreakTime") then
delay 5

else

tell application "Finder"
eject disk "10.5 Time Machine"
end tell
end if
end



i just don't know how to make the script keep going until the process has ended. Once it has ended i want it to unmount



Red Menace
Mar 14, 2012, 11:23 PM
Normally you would save your script as a stay-open application and put code that periodically checks in the idle handler (https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW7).

nicoisconfusion
Mar 15, 2012, 12:31 AM
i got it sorted in the end with this



-- settings variables for the script
set myVolumeLabel to "disk2s2"

on ApplicationIsRunning(BreakTime)
tell application "System Events" to set appNameIsRunning to exists (processes where name is ChronoSync)
return appNameIsRunning
end ApplicationIsRunning

-- Telling the OS to mount the drive
tell application "Finder"
set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[0-9]*' "
if not (disk myVolumeLabel exists) then
do shell script "diskutil mountDisk " & diskDev
end if
end tell

-- Checking if the Process is running and repeating until it is closed
if ApplicationIsRunning("BreakTime") then
repeat until ApplicationIsRunning("ChronoSync") = false
delay 10
end repeat
end if

-- ejecting the mounted disk
tell application "Finder"
eject disk "Backup"
end tell