Sharing is caring! I've been trying to create an AppleScript within Automater, to launch PlexConnect.py with administrator privileges to avoid typing in my password. HAs anyone done this yet? Would love a copy of your script, or maybe those that are good at this sort of thing can post what to do.
I have been googling for a bit, and found several threads on Apple.com from many others trying to do the same thing. Every time I try a new script or something someone else did, I get errors in Automater, with no idea how to fix.
Ok. Found a way that it will not hang (and therefore have to be force quitted) and to clean up after itself upon quit (allowing the unit to be restarted or shutdown without issue).
Below is what I have found to work:
set py to "path/to/PlexConnect.py"
set un to "username"
set pw to "password"
delay 10
do shell script "screen -A -m -d -S PlexConnect " & py user name un password pw with administrator privileges
on quit
do shell script "screen -A -m -d -S kill " & "kill ps -u root | grep [P]lexConnect | grep python | awk '{print $2}'" with administrator privileges
continue quit
end quit
The delay is in there because I found that if PlexConnect started up before PMS did, it wouldn't be able to locate the server (PlexGDM wasn't finding anything and since I haven't hard coded the IP address since the last couple of weeks of updates it didn't have anything to fall back to). You can tweek or completely remove the delay as needed.
Save the whole thing as an Application and make sure to check the "Stay open after run handler" box.
Which username and password do you enter in there? Your normal Mac login? I guess it's needed because you're simulating sudo? Still, hate putting my password in any text file, ever.
Anyone else gotten something like this to work as a launchd item? Not sure if user-based launchd items can run as admin, and I can see you don't want it launch at the boot up level (before login), as PMS won't be running yet. Unless you put a LONG delay in the script, or some time of polling?
It is. I have some code I'm working on now but it isn't quite working yet.
Here's what I've got. Trying to figure out why it isn't quite working yet.
set py to "path/to/PlexConnect.py"
set un to "username"
set theKeychainItem to "pw" #this is the name of the keychain item with your password
delay 10 #delay to ensure PMS has started
set theResult to do shell script "security 2>&1 find-generic-password -gs " & theKeychainItem
set thePassword to extractData(theResult, "password: \"", "\"", 0)
#return thePassword #for troubleshooting
do shell script "screen -A -m -d -S PlexConnect " & py user name un password thePassword with administrator privileges
on quit
do shell script "screen -A -m -d -S kill " & "kill `ps -u root | grep [P]lexConnect | grep python | awk '{print $2}'`" with administrator privileges
continue quit
end quit
on extractData(theText, theFieldName, theEndDelimiter, spaces)
set theDataStart to the offset of theFieldName in theText
if theDataStart = 0 then
return ""
else
set theDataStart to theDataStart + (length of theFieldName) + spaces
set theData to text theDataStart through end of theText
set theDataEnd to ((offset of theEndDelimiter in theData) - 1)
set theData to text 1 through theDataEnd of theData
end if
end extractData
Currently in the "why doesn't this work" stage of development.
Just kidding. Totally works. My return statement was uncommented and therefore the script was exiting before the good bits.
Here is what I have so far with some explanation on how to get it working.
Open keychain access and add a new entry with a unique item name and the password being your admin password. The account name is not used but you might as well enter your admin name for consistency.
Paste the below into a new AppleScript document and save it as an Application and check the "Stay open after run handler" box
Add it to your log in items
Allow it access to your Keychain when asked
The only concern I have is that
security
is getting access to this item not the AppleScript app itself which means if you always allow it access to that item, someone could use the security command to lookup that one record which would make your admin password available.
Below is the currently functional script with Keychain Access integration:
set py to "path/to/PlexConnect.py"
set un to "username"
set theKeychainItem to "pw" #this is the name of the keychain item with your password in it
#delay 10 #add this in or change the value as needed so that PlexConnect starts up after PMS
set theResult to do shell script "security 2>&1 find-generic-password -gs " & theKeychainItem
set thePassword to extractData(theResult, "password: \"", "\"", 0)
do shell script "screen -A -m -d -S PlexConnect " & py user name un password thePassword with administrator privileges
on quit
do shell script "screen -A -m -d -S kill " & "kill `ps -u root | grep [P]lexConnect | grep python | awk '{print $2}'`" with administrator privileges
continue quit
end quit
on extractData(theText, theFieldName, theEndDelimiter, spaces)
set theDataStart to the offset of theFieldName in theText
if theDataStart = 0 then
return ""
else
set theDataStart to theDataStart + (length of theFieldName) + spaces
set theData to text theDataStart through end of theText
set theDataEnd to ((offset of theEndDelimiter in theData) - 1)
set theData to text 1 through theDataEnd of theData
end if
end extractData
I recommend this for everyone, will make PlexConnect seamless now on my Mac Mini
Just kidding. Totally works. My return statement was uncommented and therefore the script was exiting before the good bits.
Here is what I have so far with some explanation on how to get it working.
Open keychain access and add a new entry with a unique item name and the password being your admin password. The account name is not used but you might as well enter your admin name for consistency.
Paste the below into a new AppleScript document and save it as an Application and check the "Stay open after run handler" box
Add it to your log in items
Allow it access to your Keychain when asked
The only concern I have is that
security
is getting access to this item not the AppleScript app itself which means if you always allow it access to that item, someone could use the security command to lookup that one record which would make your admin password available.
Below is the currently functional script with Keychain Access integration:
set py to "path/to/PlexConnect.py"
set un to "username"
set theKeychainItem to "pw" #this is the name of the keychain item with your password in it
#delay 10 #add this in or change the value as needed so that PlexConnect starts up after PMS
set theResult to do shell script "security 2>&1 find-generic-password -gs " & theKeychainItem
set thePassword to extractData(theResult, "password: \"", "\"", 0)
do shell script "screen -A -m -d -S PlexConnect " & py user name un password thePassword with administrator privileges
on quit
do shell script "screen -A -m -d -S kill " & "kill `ps -u root | grep [P]lexConnect | grep python | awk '{print $2}'`" with administrator privileges
continue quit
end quit
on extractData(theText, theFieldName, theEndDelimiter, spaces)
set theDataStart to the offset of theFieldName in theText
if theDataStart = 0 then
return ""
else
set theDataStart to theDataStart + (length of theFieldName) + spaces
set theData to text theDataStart through end of theText
set theDataEnd to ((offset of theEndDelimiter in theData) - 1)
set theData to text 1 through theDataEnd of theData
end if
end extractData
I followed you guide to make PlexConnect automatically start. However, does this mean that the AppleScript Icon will always be in the dock? is there a way to make this work in the background without adding any extra icons?
I also tried running the script with Platypus but my Apple TV does not respond if I use Platypus. Did I do it right?
Edit: Nevermind, I totally forgot to add the filed bundled into the script.
You could write a menulet in Objective-C using Xcode to run the same shell scripts in the AppleScript to have an iconless app but in my opinion, the icon is part of the appeal. Quit it from the dock, PlexConnect shutsdown. Click it in the dock, PlexConnect starts up. It's an indicator that PlexConnect is running and gives you a quit-friendly interface for PlexConnect.
You can always wait for the 0.1 binary app release and see how that is implemented.
When I click on PLexConnect icon on the dock, should I see the PlexConnect log window?
BTW Thanks Chris, I've been working in a MS environment since DOS 6.2 and just got a Mac mini 2 weeks ago. Thanks to your little script I learned how to:
If you want to hide the "PlexConnect" AppleScript icon from the dock try out an app called Dock Dodger that edits the app's info.plist file to hide it from the dock. Pretty nifty and allows it to run seamlessly in the background with no visual evidence.
Cool app. I’ve never seen that before. Which files did you have to bundle into the script?
I followed you guide to make PlexConnect automatically start. However, does this mean that the AppleScript Icon will always be in the dock? is there a way to make this work in the background without adding any extra icons?
I also tried running the script with Platypus but my Apple TV does not respond if I use Platypus. Did I do it right?
Edit: Nevermind, I totally forgot to add the filed bundled into the script.
Hmm, neither the AppleScript nor the Platypus script are working for me. The Platypus script launches and appears to be running in the background but uses no CPU cycles and the Apple TV trailers app can't connect.
As for the AppleScript, I get the following error: "security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain." number 44
I did create an Application password entry in the login keychain.