tell application "System Events" to set unixID to unix id of process "TextEdit"
do shell script ("renice +10 " & unixID)
if I run this I get:
tell application "System Events"
get unix id of process "TextEdit"
--> 13371
end tell
tell current application
do shell script "renice +10 13371"
--> error "renice: 13371: setpriority: Permission denied" number 1
Result:
error "renice: 13371: setpriority: Permission denied" number 1
if I change +10 to +19 I get:
tell application "System Events"
get unix id of process "TextEdit"
--> 13371
end tell
tell current application
do shell script "renice +19 13371"
--> ""
end tell
Result:
""
And all other numbers do not work either. No negative numbers work either. Also testing on other applications I get similar results.
I have wrote this script for Handbrake.app when coding the movies. You can use it for other application processes as well. As you see, some priorities requires administrator privileges.
property theApp : "TextEdit"
tell application "System Events" to set unixID to unix id of process theApp
set thePID to do shell script "ps -axww | /usr/bin/grep '[/]" & theApp & "'| awk '{print $1}' | head -1"
set newPriority to text returned of (display dialog "New priority for '" & theApp & "':" & ¬
return & "-20 Highest | +20 Lowest" default answer "0")
try
set theConfirmation to (do shell script "renice " & newPriority & " -p " & thePID)
on error
set theConfirmation to (do shell script "renice " & newPriority & " -p " & thePID with administrator privileges)
end try
display dialog ("Changed priority of '" & (theApp as string) & "':" & return & theConfirmation) buttons {"OK"} giving up after 5
I am not at the machine but can test later. However I need something that can run all alone. it needs to run completely automatic on my blind server at startup. So entering PW is not really an option.
You can make your script to automatically find the best priority, possible without administrator privileges:
property theApp : "TextEdit"
tell application theApp to launch
tell application "System Events" to set unixID to unix id of process theApp
repeat with Priority from -20 to 20
try
do shell script ("renice " & Priority & space & unixID)
return Priority
end try
end repeat
The first is to include the password in the script, although this may not be acceptable in many circumstances. I’ve never tried this with renice but I have with other utilities and it works OK.
do shell script "command" user name "me" password "mypassword" with administrator privileges
The second suggestion is to modify the sudo security policy. I’ve never done this on a mac but there’s a fair amount of information online as to the procedure to follow.
I didn’t test your command, but, this should allow you to invoke sudo and instantly remove the password from potential reuse.
set Secret to (display dialog "?" default answer "")'s text returned
if not Secret is "" then do shell script "printf " & Secret's quoted form & " | sudo -S env_reset,timestamp_timeout=0 renice -5 1997"
I’ve wanted to be able to run the asr shell utility without a password and decided to see if editing the sudoers file would do the job. I first added the following line to the sudoers file:
After rebooting I ran the following script, which worked fine with no password required. A password is still required for other utilities.
do shell script "sudo asr restore --erase --noprompt --source / --target /Volumes/Mini5"
The only issue I encountered is that my short user name (Robert) would not work and I had to use the my full user name. Based on my research, the sudoers file can be damaged, making the computer unbootable, so I had a rock-solid backup before beginning.
can I do something like get the PID by naming the application and adding it to your script. This so it automatically does the PID and I do not need to look for it every time.
I know this script is not doing what I want all on its own but could still be useful.
ChangeAgent. When I was researching the procedure employed in editing the sudoers file, I cam across numerous posts that said something like “I carefully followed the instructions and now my computer won’t boot.” So, I understand your reticence. Hopefully another forum member will have a usable solution.
Yes. Your first code sample is—more or less—the way to do that; I just added some privilege escalation and error checks.
set unixID to null
tell application "System Events" to try
set unixID to process "TextEdit"'s unix id
end try
set Secret to (display dialog "?" default answer "")'s text returned
if not Secret is "" and unixID is not null then do shell script "printf " & Secret's quoted form & " | sudo -S env_reset,timestamp_timeout=0 renice -5 " & unixID
The PID and the unix ID is the same thing. So, when you get the unix id in your original script, you get the pid.
All negative priorities require administrator privileges in any case. A showed you workaround for not negative priorities, because you asked for priority like +10.
NOTE: The Secret of Marc Anthony’s script is not big secret, because 1) it is not hidden and 2) he uses assigning to variable Secret. So:
property theApp : "TextEdit"
property thePriority : -5
tell application "System Events"
if not (exists process theApp) then return
set unixID to unix id of process theApp -- or: set thePID to unix id of process theApp
end tell
do shell script "printf " & ((display dialog "" with title "Enter the password" default answer "" with hidden answer)'s text returned)'s quoted form & " | sudo -S env_reset,timestamp_timeout=0 renice " & thePriority & space & unixID
These are moot points. My variable doesn’t persist, and, whether or not the text is “hidden,” it’s in plain sight in the event log. Unless you’re planning on someone sitting directly behind you, hiding text during entry is a user-hostile interface design practice. Standard Additions also don’t belong inside shell calls, unless they’re escaped.
Just to clarify, changing a sudo security preference as demonstrated in my post only removes the password requirement for the specified app. For me this was asr and for the OP this is renice.
When the OP stated “a bit to risky for me” I assumed he meant modifying the sudoers file. Removing the password requirement for the renice shell command would not seem a major security risk. Removing the password requirement for the asr shell command is probably a more significant security risk but it’s one I can live with.