Killing all Adobe Background processes when not needed MAC - macos

This code and question is following on from perfectfiasco question and answer by Ted Wrigley.
I too find it crazy how many processes Adobe runs in the background - even when finder extensions are disabled and not using any apps.
I am trying to create something similar but for 3 additional apps also Lightroom / creative cloud and adobe bridge also and have edited it slightly to include all the other processes that Adobe run in the background. Any idea if I have added these correctly?
Not entirely sure how to separate apps from processes and if its necessary to killall for apps or just quit command?
Is it fine to force quit process and apps like this?
Something I have noticed is that if lots of adobe processes are quit then it launches this daemon "AdobeCRDaemon" is that expected?
Not sure what to put here now several other processes have been added - tell process "AGMService"
Does anything else in the code need updating for current macOS Monterey ?
Thank you in advance for any clarification
use AppleScript version "2.4"
use framework "AppKit"
use scripting additions
property NSWorkspace : class "NSWorkspace"
on run
set workSp to NSWorkspace's sharedWorkspace()
set notifCent to workSp's notificationCenter()
tell notifCent to addObserver:me selector:"someAppHasTerminated:" |name|:"NSWorkspaceDidTerminateApplicationNotification" object:(missing value)
end run
on idle
-- we don't use the idle loop, so tell the system let the app sleep. this comes out of idle once an hour
return 3600
end idle
on someAppHasTerminated:notif
set termedApp to (notif's userInfo's valueForKey:"NSWorkspaceApplicationKey")
set termedAppName to (termedApp's localizedName) as text
-- I'm guessing at the localized names for Photoshop and Illustrator. you may need to alter these
if termedAppName is "Adobe Photoshop 2022" or termedAppName is "Adobe Lightroom Classic" or termedAppName is "Adobe Bridge 2022" or termedAppName is "Adobe Lightroom" or termedAppName is "Creative Cloud" then
-- close the service here
tell application "System Events"
tell process "AGMService"
if its accepts high level events is true then
tell application "AGMService" to quit
tell application "Adobe Desktop Service" to quit
tell application "CCLibrary" to quit
tell application "CCXProcess" to quit
tell application "Core Sync" to quit
tell application "Creative Cloud Helper" to quit
else
do shell script "killall AGMService"
do shell script "killall Adobe Desktop Service"
do shell script "killall CCLibrary"
do shell script "killall CCXProcess"
do shell script "killall Core Sync"
do shell script "killall Creative Cloud Helper"
end if
end tell
end tell
end if
end someAppHasTerminated:

This is a deeply unsafe way to do this, but I've replaced the nested tell hell in onAppHasTerminated this:
do shell script "pkill -i -f adobe"
My personal script looks like this.

I had the same problem.
Just run this command in terminal:
kill $(ps aux | grep -i '.adobe' | grep -v grep | awk '{print $2}')

Related

how to close an active document in Mac OS using shell script

I am closing active document using apple script as below
tell application "Microsoft Word"
activate
try
if not (exists active document) then error number -128
close active document saving yes
on error
end try
end tell
want to do similar action using shell script. I want to gracefully close it and don't want to use kill command . And I don't want to use osascript to call an apple script . I want a graceful way using native shell commands
Hi you can use osascript in the shell code
#!/bin/sh
osascript <<EOF
tell application "$1"
close (every window whose name is "$2")
end tell
EOF
Compile this code and make filename.sh
or you can use
#!/bin/sh
osascript <<EOF
tell application "Preview"
close (every window whose name is "$1")
end tell
EOF
To use this from cmd type $./file_name Preview application name
Here I used the reference of https://ss64.com/osx/osascript.html

Get PIDs of all open windows on MacOS

I tried to use the following AppleScript to get the PIDs of all the windows (including ones that are minimized). This script doesn't get the PIDs of windows on other desktops.
Is there any workaround for this so I can still get a list of opened windows across all desktops without having to activate individual processes and checking if they have windows?
tell application "System Events"
repeat with proc in (every process)
if exists(first window of proc) then
set pid to unix id of proc
log pid
end if
end repeat
end tell
PS, I'm not too proficient with AppleScript. I've managed to hack this together using StackOverflow. This might not be the most efficient way to do what I'm trying to do.
Looks like I got this to work with this ugly bash-applescript hack
osascript -e "tell application \"System Events\"
repeat with proc in (processes where background only is false)
set pname to name of proc
log pname
end repeat
end tell" 2>&1 |
while read line
do
echo "process " $line
pgrep $line
done
This prints something like
process Finder
818
process Google Chrome
3730
3734
3740
5838
process iTerm2
3750
4210
process Sublime Text
3822
Where PID 818 belongs to the Finder process and so on.

Access built-in Power Manager? states

Im trying to write a super simple applescript that will launch the OneDrive App, or ensure it is open, whenever the machine's power source is set to plugged in, and will quit, or make sure is closed, when the power source is set to battery.
I'm having trouble finding how to access the built-in "power indicator" in Yosemite. All of my searches lead to old, irrelevant results from years ago.
Edit: I think I will have to use a do shell script within the applescript using pmset -g batt
Now drawing from 'AC Power'
-InternalBattery-0 100%; charged; 0:00 remaining
And parse this result, but I am not sure how.
Edit: Here it is for anyone in the future who may want something similar:
global appName
on appIsRunning()
tell application "System Events" to (name of processes) contains appName
end appIsRunning
on acIsConnected()
return (do shell script "system_profiler SPPowerDataType | grep -q 'Connected: Yes' && echo \"true\" || echo \"false\"") as boolean
end acIsConnected
on toggleApp()
if my acIsConnected() then
if not my appIsRunning() then
tell application "Finder"
open application file (appName & ".app") of folder "Applications" of startup disk
end tell
end if
else
tell application appName
quit
end tell
end if
end toggleApp
-- This will only be executed once.
on run
set appName to "OneDrive"
end run
-- This will be executed periodically, specified in seconds, every return.
on idle
my toggleApp()
-- Execute every 2 minutes.
return 120
end idle
-- Not mandatory, but useful for cleaning up before quiting.
on quit
-- End handler with the following line.
continue quit
end quit
Here is a one-liner that polls for connected status, since I guess you can have less than 100% and still be connected (charging).
set acConnected to (do shell script "system_profiler SPPowerDataType |grep -q 'Connected: Yes' && echo \"true\" || echo \"false\"") as boolean
Here's another one liner...
set acConnected to last word of paragraph 1 of (do shell script "ioreg -w0 -l | grep ExternalChargeCapable")
If you are happy to use a third party tool, you can avoid polling for the battery state. This will make your script more efficient.
Power Manager can run AppleScripts when the battery state changes. How to Run a Command When Switching to Battery Power, walks through how to set this up for scripts.
Swap out the #!/bin/sh for #!/usr/bin/osascript in the script, and you can use AppleScript.
Disclaimer: I wrote Power Manager and can answer comments about how it works.
Provided you have battery icon on screen's top right:
tell application "System Events" to tell process "SystemUIServer" ¬
to value of attribute "AXDescription" of ¬
(first menu bar item whose value of attribute "AXDescription" ¬
begins with "Battery") of menu bar 1
You get "Battery: Charged" or "Battery: Calculating Time Remaining… " or something else

Can't make Unix ID of Process into text?

I have a modbook with a glitchy digitizer board. Until I can re-shield the cable that is causing the glitch I just want to turn the digitizer off. I found a page which taught me some code and I used it successfully last night. Upon reboot, however, it doesn't work anymore:
The script:
tell application "System Events"
set PTD to (unix id of process "PenTabletDriver") as text
do shell script "kill -STOP " & quoted form of (PTD)
end tell
The error message:
error "Can’t make «class idux» of «class prcs» \"PenTabletDriver\" of
application \"System Events\" into type text." number -1700 from
«class idux» of «class prcs» "PenTabletDriver" to text
Can I alter the code somehow to fix this problem?
PS:
I have read this post and, though it's similar, I do not understand how it could be applied to my problem.
Life is too short to mess with AppleScript. Try running the following command at the terminal prompt:
pkill -STOP PenTabletDriver
Also, check your login items to see if the driver is automatically started each time you log in. (More likely, though, is that it's configured to start at boot time via launchd.)
You can save the following script in a little applet:
set shellStr to "pkill -STOP PenTabletDriver"
do shell script shellStr
and run it whenever you need.
I think the other answers make a good point, a shell command is nice and quick. BUT, if you must have it in AppleScript, this seems to work for me....
tell application "System Events"
set PTD to (unix id of process "iTunes")
do shell script "kill -STOP " & quoted form of (PTD as text)
end tell
which results to
tell application "System Events"
get unix id of process "iTunes"
--> 37987
do shell script "kill -STOP '37987'"
--> error number -10004
end tell
tell current application
do shell script "kill -STOP '37987'"
--> ""
end tell
The Process ID is just a number so there is no need to quote it...
tell application "System Events"
set PTD to (unix id of process "iTunes")
do shell script "kill -STOP " & PTD
end tell
The above code is sufficient.

Script to shutdown mac

I'm trying to automate the shutdown of my mac, I've tried the scheduled shutdown in energy saver and I wanna sleep but these don;t seem to work. VLC player runnign seems to prevent the shutdown. I think I need a script to forcefully shutdown the mac regardless of of what errors may thrown to screen by various programs running.
Thanks
Ok,
This is the applescript code im using to shutdown may mac. I've added it as an iCal event thats runs nightly.
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
do shell script "killall \"" & this_process & "\""
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
tell application "System Events"
shut down
end tell
Could you try a simple applescript, which goes something like this...
tell application "System Events"
shut down
end tell
See if it works, and then you can make it run through Automator at certain time, etc.
my solution (somwhat late). Just a bash script with apple in it:
#!/bin/bash
# OK, just shutdown all ... applications after n minutes
sudo shutdown -h +2 &
# Try normal shutdown in the meantime
osascript -e 'tell application "System Events" to shut down'
I also edited the /etc/sudoers (and /private/etc/sudoers) file(s) and added the line:
ALL=NOPASSWD: /sbin/shutdown
Always worked for me for an assured shutdown (knock knock ;-) )
This should do:
do shell script "shutdown" with administrator privileges
If you want to pass the admin password from key chain, with no prompt:
do shell script "shutdown" with administrator privileges password "password here"
But do not store the admin password in clear anywhere. Instead use the keychain access.
Alternatively you could kill all user processes, via:
do shell script "kill -9 -1"
This however would also kill your own Applescript process, preventing it from requesting the shutdown/restart afterwards.
Either way you're playing with fire, when using sudo or kill.
do what linux users do. use a bash script. if u dont know how to create one just go ahead and download ANY bash script u find using your internet search and open it with text edit app and paste the following:
( be careful if many people use the pc , then this method is not recommended, cause they can learn your user login password from inside this script )
#!/bin/bash
echo -n "Enter a number > "
read x
echo [your password] | sudo -S shutdown -h +$x
it will work the same way it works in linux. the terminal will pop up a message and ask you to enter a number. if we choose for exaple 50 , then the pc ( niresh ) or mac will shutdown in 50 minutes.

Resources