Applescript Runs twice?

Discussion in 'Mac Programming' started by thewatts, May 18, 2009.

  1. thewatts macrumors newbie

    Joined:
    May 18, 2009
    #1
    so, writing a code to send me a text when a program [VisualHub] quits after converting video files.

    the issue is that when i initially run the script, it follows through with running the second script called FilesDone [one which i made with automator].

    the script is a repeating check to see if visualhub is present in the running applications. if so - keep checking. if not - then run [FilesDone], end repeat.

    Code:
    on run {input, parameters}
    	
    	repeat
    		tell application "System Events"
    			if ((get name of the processes) does not contain "VisualHub") then
    				tell application "FilesDone"
    					activate
    					exit repeat
    				end tell
    			end if
    		end tell
    	end repeat
    	
    	
    	return input
    end run
    VisualHub is running when i initially run the script. yet - it runs FilesDone anyway, but keeps checking to see if VisualHub is closed [and when closes - runs FilesDone again].

    what have i missed that FilesDone is executed at the start of the repeat?

    thanks,
     
  2. neutrino23 macrumors 65816

    Joined:
    Feb 14, 2003
    Location:
    SF Bay area
    #2
    I'm not sure why this is happening.

    I suggest putting in a display dialog statement to show you what is being returned.

    Once you see what is being returned you can modify the test.

    Suggestion: I'd add a small delay of a few seconds so you aren't hammering the system checking if the program is still running or not.

    -- delay for n seconds
    delay n
     
  3. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    Definitely a good suggestion. A great deal of debugging gets done that way.

    (BTW, you have proven that the process's name is "VisualHub" and a typo isn't just causing the issue? Just asking ...)

    I'd also consider rewriting that if...then clause to make it positive and see if something shakes out, such as

    Code:
    if ((get name of the processes) contains "VisualHub") then
        -- nothing, maybe add a beep here. It'll drive you nuts but it works
        else
    	tell application "FilesDone"
    		activate
    		exit repeat
    	end tell
    end if
    I'm also a little curious about the "exit repeat" inside the tell statement. i wonder if it might not be better to do something like this:

    Code:
    if ((get name of the processes) contains "VisualHub") then
        -- nothing
        else
    	tell application "FilesDone"
    		activate
    	end tell
    	exit repeat
    end if
     
  4. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    I would put a delay in the loop as running that check continuously may starve the CPU.

    EDIT: Oops, sorry, neutrino already mentioned that... :rolleyes:
     

Share This Page