Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SamAndrews

macrumors newbie
Original poster
Jul 27, 2014
3
0
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
 
Last edited:

numero

macrumors regular
Jul 23, 2002
106
3
OR
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.
https://forums.macrumors.com/threads/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
 

SamAndrews

macrumors newbie
Original poster
Jul 27, 2014
3
0
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.
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.