help with applescript

Discussion in 'Mac Programming' started by nicoisconfusion, Mar 14, 2012.

  1. macrumors newbie

    Joined:
    Sep 27, 2011
    #1
    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.


    Code:
    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
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    Normally you would save your script as a stay-open application and put code that periodically checks in the idle handler.
     
  3. thread starter macrumors newbie

    Joined:
    Sep 27, 2011
    #3
    i got it sorted in the end with this

    Code:
    
    -- 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
    
     

Share This Page