AppleScript [Running in Background]

Discussion in 'Mac Programming' started by SamAndrews, Jul 27, 2014.

  1. SamAndrews, Jul 27, 2014
    Last edited: Jul 27, 2014

    SamAndrews macrumors newbie

    Joined:
    Jul 27, 2014
    #1
    Hi Guys,

    I have an AppleScript that I have created. I was wondering how I can keep it running in the background when I start my Mac and to automatically close "PDApp" when "Adobe Flash CC 2014" is open and not for me to manually execute the script each time "Adobe Flash CC 2014" is open. Thanks.
    I hope you guys can help :)

    Script.

    on run {input, parameters}

    tell application "System Events"
    set ProcessList to name of every process
    if "Adobe Flash CC 2014" is in ProcessList then
    set ThePID to unix id of process "PDApp"
    do shell script "kill -KILL " & ThePID
    end if
    end tell

    return input
    end run
     
  2. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #2
    The fast way to get this working might be to do this.

    Code:
    on run {input, parameters}
    	repeat
    		tell application "System Events"
    			set ProcessList to name of every process
    			if "Adobe Flash CC 2014" is in ProcessList then
    				set ThePID to unix id of process "PDApp"
    				do shell script "kill -KILL " & ThePID
    			end if
    		end tell
    		delay 60 -- Choose how often you want script to wake and do a check. 
    			 -- Time is in seconds.
    	end repeat
    	return input
    end run
    Save as an application and put it in your login items (System Prefs / User and Groups / Login Items).

    Then I started to remember a really elegant way I was shown in this thread.
    http://forums.macrumors.com/showthread.php?t=1540881

    If you go this route you will want to probably read about NSWorkspaceDidLaunchApplicationNotification here:
    https://developer.apple.com/library...es/nsworkspace_class/reference/reference.html

    -numero
     
  3. SamAndrews thread starter macrumors newbie

    Joined:
    Jul 27, 2014
    #3
    Thank you :) I'll check it out now.

    ----------

    It works, thanks so much.

    ----------

    Also, just a quick question how can I make it so when "PDApp" isn't in the Process List it will not run the scrip? Thanks.
     
  4. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #4
    In either the case of my supplied code or the idea in the link I provided the script must be running at all times.

    In the code I provided the "delay xx" line is where the script sleeps for the specified number of seconds before running through the loop again to check for the application. That keeps the script from eating up unnecessary amounts of CPU time. A delay of 1 second is probably not reasonable. Do you really need to know within 1 second of an app being launched that it is running? On the other end 3600 (1 hour) is probably too high. Pick the highest number that gives you the performance you can live with. Most of the time the script will wake up and not find the app active.

    In the linked example the OS does the waiting for you. The script sits idle until the OS notices that the event you requested happened and then your function is called.

    If the script isn't running the logic cannot be executed.
     

Share This Page