Has anyone created an AppleScript/Application to launch PlexConnect at startup?

Hi, Here's how i do it using launchctl on OSX. Works a treat, no ugly icons or anything. It launches on startup, and keeps re-trying to launch until it succeeds in connecting to PMS, so it doesn't matter if it manages to start before PMS.
 
First, we need to make it quit if it manages to start before PMS, so launchctl can retry in 10s. Open up PlexGDM.py and search for the line 'No servers discovered'. Insert a line below this, at the same indentation, and add: sys.exit(1)
 
Next, we need to make it work without any access to the keyboard. Open up PlexConnect.py, look for the line that says 'try:', and delete this line *and every other line after it*. Don't worry, all you're removing is the ability for it to cleanly shut down if the user presses a key.
 
Next, create com.plex.plexconnect.plist with the following contents: https://gist.github.com/chrishulbert/5772931
Edit this file, changing the *two* instances of 'Users/chris/PlexConnect' to the correct folder that you've installed PlexConnect in.
 
Finally, go into the terminal, change into your plex connect folder, and do the following:
 
sudo cp com.plex.plexconnect.plist /Library/LaunchDaemons/
sudo launchctl load /Library/LaunchDaemons/com.plex.plexconnect.plist
sudo launchctl start com.plex.plexconnect
 
Voila!

Awesome this is SO much easier then trying to use automator.

Thanks

Hi Chris

 

and thanks for plexconnect

 

I followed your instructions. But when i enter the command sudo launchctl start com.plex.plexconnect i get an error stating: launchctl start error: Bad file descriptor

What am i doing wrong?

When i go back and do this command again: sudo launchctl load /Library/LaunchDaemons/com.plex.plexconnect.plist it says: com.plex.plexconnect: Already loaded

So i guess im good so far?

Thanks for your help!  :)

Great post and I like the solution. However when I load the plist i get:

18/06/13 11:33:14.849 AM com.apple.launchd[1]: (com.plex.plexconnect[20043]) Exited with code: 1
 
Any thoughts?

This works but it orphans the three Python processes and won't let your system shutdown unless you force quit the Application.

set py to "path/to/PlexConnect.py"
set un to "username"
set pw to "password"
do shell script py user name un password pw with administrator privileges

Working on that issue now.

 sorry i am new to this, what is py, un and pw?

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?

PlatypusPlex.jpg

Edit: Nevermind, I totally forgot to add the filed bundled into the script. 

What filed bundled did you add into the script?

What filed bundled did you add into the script?


The contents (minus the .pyc files) of the PlexConnect folder.

Honestly though, you'll probably be better off using the launchctl plist posted by chulbert.

Chulbert1234,


Any idea why your launchctl script fails to launch the correct and newer version of Python when PlexConnect.py uses ‘/usr/bin/env python’ instead of ‘/usr/bin/python’? Specifically, I have Python 2.6.1 as the system default Python installed on my system. But this version has problems with PlexConnect, so I installed Python 2.7.5. If I use an absolute path in PlexConnect.py to the new the newer Python (#!/usr/local/bin/python), your launchctl script works perfectly. But if I use the recommended shebang (#!/usr/bin/env python), the launchctl script launches the older version of Python. If I launch the PlexConnect.py script manually, it uses the newer version as it should.


Over in this thread, institute11 posted something he found that might point to the cause of and solution to (if there is one) the problem with running a python script at startup if it uses ‘/usr/bin/env python’:

This is from a python.org mail thread. Maybe it explains why it doesn’t work with launchctl.


One of the main dis-advantages to using ‘/usr/bin/env python’ is that

you have to either A. make sure the environment is initialized, or B.

initialize the environment manually before executing the script. If

you, for example, want to use a python script at boot time, before the

environment is initialized, i strongly recommend using an absolute path.

“#!/usr/bin/env python” vs “#!/usr/local/bin/python”




Thoughts?

I installed the latest version of plexconnect which changed the path to use /usr/bin/env python but I still get the error 1 when trying to load the plex connect.plist

2013-06-20 21:17:26,135 com.apple.launchd[1]: (com.plex.plexconnect[10870]) Exited with code: 1
 
Running stand alone is no problem though. 
 
 
Edit: Changed the path from /usr/dev/env python to what it actually is which is /usr/bin/env python

Did you edit the file to “/usr/dev/env python”? Because I think that should actually be “/usr/bin/env python”.

I installed the latest version of plexconnect which changed the path to use /usr/dev/env python but I still get the error 1 when trying to load the plex connect.plist



2013-06-20 21:17:26,135 com.apple.launchd[1]: (com.plex.plexconnect[10870]) Exited with code: 1


Running stand alone is no problem though.

If I run this command in Terminal /usr/bin/env python it opens Python version 2.7.2 which should be ok with PlexConnect. That is also what I see in the PlexConnect.py file. BTW in previous post that was a mistake on my part. It actually says /usr/bin/env python. 

 

#!/usr/bin/env python
 
"""
PlexConnect
 
Sources:
inter-process-communication (queue): http://pymotw.com/2/multiprocessing/communication.html
"""

For some reason the launchd plist provided above doesn’t work with “/usr/bin/env python” so you’ll have to edit the first line of PlexConnect.py with the absolute path of your Python install, either “/usr/bin/python” or “/usr/local/bin/python”. In your case, it should be “/usr/bin/python”.

If I run this command in Terminal /usr/bin/env python it opens Python version 2.7.2 which should be ok with PlexConnect. That is also what I see in the PlexConnect.py file. BTW in previous post that was a mistake on my part. It actually says /usr/bin/env python.


#!/usr/bin/env python “”“PlexConnect Sources:inter-process-communication (queue): [http://pymotw.com/2/multiprocessing/communication.html](http://pymotw.com/2/multiprocessing/communication.html)”“”

Ok changed the first line in PlexConnect.py to #!/usr/bin/python

Ran this command in Terminal:

sudo launchctl start com.plex.plexconnect

Console reads:

2013-06-20 22:21:05,597 com.apple.launchd[1]: (com.plex.plexconnect[11217]) Exited with code: 1

I got it working. I downloaded the plexconnect files again. Edited them like was described and now it works. I must have made a small editing mistake. 

BTW I changed the path back to /usr/bin/env python and it still works. Must have been an editing mistake in the PlexGDM.py file.

Sadly, it appears that chulbert1234's launchctl script will ignore any user set PATH variables when using "#!/usr/bin/env python" in PlexConnect.py. It seems that launchctl doesn't look beyond "PATH="/usr/bin:/bin:/usr/sbin:/sbin". So, if you have a newer version of Python installed in "/usr/local/bin", chulbert1234's launchctl plist will ignore that path and use OS X's default Python install at "/usr/bin".

Now this isn't too big of a deal for Lion and Mountain Lion users because they have Python 2.7 installed by default, but for those of use still on Snow Leopard, it means that we'll see the annoying graphical metadata glitches (i.e., "((ERROR:VAL))") when viewing video info about actors, directors, and producers in PlexConnect.

The only way around this launchctl restriction that I can find is to edit the PlexConnect files to use absolute paths to any newer user-installed Python (e.g., /usr/local/bin/python) in the shebang line that starts each PlexConnect file. It's inconvenient, but if you use Search-and-Replace in TextWrangler or BBedit, it's not too bad.

Sadly, it appears that chulbert1234's launchctl script will ignore any user set PATH variables when using "#!/usr/bin/env python" in PlexConnect.py. It seems that launchctl doesn't look beyond "PATH="/usr/bin:/bin:/usr/sbin:/sbin". So, if you have a newer version of Python installed in "/usr/local/bin", chulbert1234's launchctl plist will ignore that path and use OS X's default Python install at "/usr/bin".

Now this isn't too big of a deal for Lion and Mountain Lion users because they have Python 2.7 installed by default, but for those of use still on Snow Leopard, it means that we'll see the annoying graphical metadata glitches (i.e., "((ERROR:VAL))") when viewing video info about actors, directors, and producers in PlexConnect.

The only way around this launchctl restriction that I can find is to edit the PlexConnect files to use absolute paths to any newer user-installed Python (e.g., /usr/local/bin/python) in the shebang line that starts each PlexConnect file. It's inconvenient, but if you use Search-and-Replace in TextWrangler or BBedit, it's not too bad.

You could try a technique called chain loading to load the PlexConnect script at boot time with an extended environment i.e. you get launchtl to call a standard shell script which in turn sets the environment up correctly and then calls the PlexConnect script.

Hi, Here's how i do it using launchctl on OSX. Works a treat, no ugly icons or anything. It launches on startup, and keeps re-trying to launch until it succeeds in connecting to PMS, so it doesn't matter if it manages to start before PMS.
 
First, we need to make it quit if it manages to start before PMS, so launchctl can retry in 10s. Open up PlexGDM.py and search for the line 'No servers discovered'. Insert a line below this, at the same indentation, and add: sys.exit(1)
 
Next, we need to make it work without any access to the keyboard. Open up PlexConnect.py, look for the line that says 'try:', and delete this line *and every other line after it*. Don't worry, all you're removing is the ability for it to cleanly shut down if the user presses a key.
 
Next, create com.plex.plexconnect.plist with the following contents: https://gist.github.com/chrishulbert/5772931
Edit this file, changing the *two* instances of 'Users/chris/PlexConnect' to the correct folder that you've installed PlexConnect in.
 
Finally, go into the terminal, change into your plex connect folder, and do the following:
 
sudo cp com.plex.plexconnect.plist /Library/LaunchDaemons/
sudo launchctl load /Library/LaunchDaemons/com.plex.plexconnect.plist
sudo launchctl start com.plex.plexconnect
 
Voila!

Hey any change of updating this for the latest version?

please, i don't understand.

can anyone please pot a guide with photos about how to make the app first ? i'm not in the applescript step even yet !

please guys

Read or re-read post #7 in this thread. It gives you a detailed step-by-by about how to do it.

Also, Keychain Access is an app in the applications/utilities folder of your Mac.

Also, Keychain Access is an app in the applications/utilities folder of your Mac.

i know that

Read or re-read post #7 in this thread. It gives you a detailed step-by-by about how to do it.

i can't even make the app ! i read all of the posts here and i still don't understand.