# Can you run AppleScript within a plugin?

**URL:** <https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [June 24, 2009, 10:26am UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082 "2009-06-24T10:26:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![oortmanp](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oortmanp](https://forums.plex.tv/u/oortmanp)\
**Post date:** [June 24, 2009, 10:26am UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/1 "2009-06-24T10:26:14Z")

</div>

Hi  
  
  
  
I am trying to create a plugin for iChat in plex which needs some applescript coding. Now I know that python can execute some applescript via e.g. :

> [@](#):
>
> import os  
>   
> cmd = “”“osascript -e ‘tell app “Finder” to sleep’”“”  
>   
> def stupidtrick():  
>   
> os.system(cmd)  
>   
> stupidtrick()

  
  
  
  
I also found [https://plexapp.lighthouseapp.com/projects/...-skin-xml-files](https://plexapp.lighthouseapp.com/projects/14382/tickets/119-add-scripting-directly-in-skin-xml-files)  
where a ticket is opened for python code in xml files (still status new?)  
  
  
  
And in [post="0"]While there is NO Plug-In development guide out there yet ...[/post] I read at the bottom of the first Post [quote] Missing items within this DOCUMENTATION:‎  
  
‎- "exotic / advanced" things one can do from within a plug-in (e.g. run an ‎  
EXTERNAL function (dll etc.) / start - stop other programs, multithreading)‎

[/quote]  
  
  
  
  
So now my question is: in what way can I include applescript code in “ **init**.py” that will be executed when I select an item like a Media-Item  
  
Can anybody help me?  
  
  
  
thanks  
  
oortmanp

---

<div class="post-metadata">

**Author:** ![elan](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/elan/32/5_2.png) [@elan](https://forums.plex.tv/u/elan)\
**Post date:** [June 24, 2009, 7:54pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/2 "2009-06-24T19:54:19Z")

</div>

> [@](#):
>
> So now my question is: in what way can I include applescript code in “ **init**.py” that will be executed when I select an item like a Media-Item

  
  
We prefer you don't use os.system, it would probably be better to use some of the built-in (on Leopard) Python/Applescript (see [http://pyobjc.sourceforge.net/)](http://pyobjc.sourceforge.net/)).  
  
In order to execute it when hitting a media item, you can use the v1 of the framework and use a Function() item to call back into your plug-in. Check out any of the myriad of v1 plug-ins for good example code.  
  
Let me know if you have any more questions :)

---

<div class="post-metadata">

**Author:** ![jam](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jam/32/186050_2.png) [@jam](https://forums.plex.tv/u/jam)\
**Post date:** [June 24, 2009, 11:19pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/3 "2009-06-24T23:19:43Z")

</div>

I’ve been wondering about the best way of providing AppleScript support in plug-ins. There’s an event bridge (py-appscript) that looks fairly promising, but it’s still in the very early stages of development, so may not be appropriate (I haven’t spent a great deal of time looking in to it - AppleScript support will be useful for a feature that’s a long way off yet, so it’s not top priority :))

---

<div class="post-metadata">

**Author:** ![Gerk](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/gerk/32/81980_2.png) [@Gerk](https://forums.plex.tv/u/Gerk)\
**Post date:** [July 14, 2009, 3:25pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/4 "2009-07-14T15:25:01Z")

</div>

I was also wondering about the same thing, applescript within python. Is pyobjc stuff bundled with Leopard now? Also do you know of any good places with examples of using the leopard built-in’s and applescript? Got a little pet project I’ve been working on for my own usage that I’d like to also get working in plex at some point.  
  
  
  
We ever gonna see some dev docs? 😉

---

<div class="post-metadata">

**Author:** ![billygoat23](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@billygoat23](https://forums.plex.tv/u/billygoat23)\
**Post date:** [November 9, 2009, 7:25pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/5 "2009-11-09T19:25:48Z")

</div>

Howdy, I’m new to Plex (as of a week ago), but it’s pretty awesome.  
  
  
  
I write a lot of Python for my job (mostly for writing cross platform testing harnesses and the like) and though I haven’t written any python for Plex yet, I do have some experience with Python and AppleScript… The py-appscript stuff works quite well. The hardest part is translating the AppleScript code into what it should be for Python. Here’s a quick example (launch Safari from python and then tell Safari to load a url):  
  
  
  
from appscript import \*  
  
import commands  
  
theUrl = “[http://google.com](http://google.com)”  
  
app( “Safari.app” ).launch()  
  
app( “Safari” ).open\_location( theUrl )  
  
  
  
I expect that scripting iChat would be similar. Good luck with your project.

---

<div class="post-metadata">

**Author:** ![Gerk](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/gerk/32/81980_2.png) [@Gerk](https://forums.plex.tv/u/Gerk)\
**Post date:** [November 9, 2009, 7:40pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/6 "2009-11-09T19:40:21Z")

</div>

> [@](#):
>
> Howdy, I'm new to Plex (as of a week ago), but it's pretty awesome.  
>   
> I write a lot of Python for my job (mostly for writing cross platform testing harnesses and the like) and though I haven't written any python for Plex yet, I do have some experience with Python and AppleScript... The py-appscript stuff works quite well. The hardest part is translating the AppleScript code into what it should be for Python. Here's a quick example (launch Safari from python and then tell Safari to load a url):  
>   
> from appscript import \*  
> import commands  
> theUrl = "http://google.com"  
> app( "Safari.app" ).launch()  
> app( "Safari" ).open\_location( theUrl )  
>   
> I expect that scripting iChat would be similar. Good luck with your project.

  
  
Is the py-appscript bundled with Leopard or is it required to add manually? I'd just be concerned that if we want to include something like this and users don't have it on their system it would require too much hoop jumping for them ...

---

<div class="post-metadata">

**Author:** ![jam](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jam/32/186050_2.png) [@jam](https://forums.plex.tv/u/jam)\
**Post date:** [November 9, 2009, 8:37pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/7 "2009-11-09T20:37:20Z")

</div>

We already use a bunch of stuff that isn’t bundled with OS X - that’s not an issue 🙂 The plug-in framework & App Store make it very easy to push updates out to all users, including any new libraries we may come to rely upon. My only concern is the potential security implications - we’ll soon be starting to sandbox running plug-ins, preventing them from accessing the underlying system or unsafe modules. Allowing plug-ins to break out of the sandbox & run any AppleScript they like could cause all sorts of problems!

---

<div class="post-metadata">

**Author:** ![oortmanp](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oortmanp](https://forums.plex.tv/u/oortmanp)\
**Post date:** [January 8, 2010, 3:15pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/8 "2010-01-08T15:15:24Z")

</div>

Finally I got my macbook and it looks like now I have some more time for programming.  
  
So I checked this topic again and read about the py-appscript solution (pretty easy with the translator from their website [http://appscript.sourceforge.net/tools.html](http://appscript.sourceforge.net/tools.html) )  
  
  
  
Also I took (again) a deeper look into Elan’s statement about PyObjC. For me this looks a bit more complicated. But okay if one of the two works, it’s fine.  
  
  
  
I searched the net for some code and found that either:  
  
“from appscript include \*” for the py-appscript and also  
  
“from InstantMessage import \*” for PyObjC  
  
would stop a plugin from loading. (Both lines work from the python command line)  
  
  
  
I found the PyObjC code at:  
  
[http://pyobjc.sourceforge.net/examples/pyobjc-framework-Automator/Get%20Buddy%20Info/source--GetBuddyInfo.py.html](http://pyobjc.sourceforge.net/examples/pyobjc-framework-Automator/Get%20Buddy%20Info/source--GetBuddyInfo.py.html)  
  
  
  
So for now, I got no further in programming the plugin.  
  
How can I include the py-appscript into plex. I read something in the PlexMediaFramework.pdf of “The Libraries directory”. Is it possible to put some py-appscript code there (and when yes, what exactly)? As you may read, I’m totally lost (and don’t program a lot)  
  
  
  
Can somebody point me into the right direction?  
  
  
  
thanks  
  
oortmanp

---

<div class="post-metadata">

**Author:** ![jam](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jam/32/186050_2.png) [@jam](https://forums.plex.tv/u/jam)\
**Post date:** [January 9, 2010, 12:39pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/9 "2010-01-09T12:39:28Z")

</div>

You can create a directory called “Libraries” inside your plug-in’s “Contents” directory. Any Python libraries you put in this directory will be available to your code.

---

<div class="post-metadata">

**Author:** ![oortmanp](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oortmanp](https://forums.plex.tv/u/oortmanp)\
**Post date:** [January 10, 2010, 8:01pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/10 "2010-01-10T20:01:52Z")

</div>

> [@](#):
>
> You can create a directory called "Libraries" inside your plug-in's "Contents" directory. Any Python libraries you put in this directory will be available to your code.

  
  
Hi Jam,  
  
if I could place my appscript library there, against which version of Plex does it have to be compiled? Correct me if I am wrong, but for as I know, is OS X 10.6 shipped with Python 2.6 and OS X 10.5 shipped with 2.5  
What does Plex use? Does it use the build in of OS X or does it come with it's own? And if so, which version?  
Or am I thinking way to complicated?  
  
Thanks for your help  
  
oortmanp

---

<div class="post-metadata">

**Author:** ![jam](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jam/32/186050_2.png) [@jam](https://forums.plex.tv/u/jam)\
**Post date:** [January 10, 2010, 10:04pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/11 "2010-01-10T22:04:22Z")

</div>

I asume you mean “against which version of _Python_ does it have to be compiled”? 😉  
  
  
  
We target the Python 2.5 installation that ships with OS X. Although OS X 10.6 includes Python 2.6, it still includes 2.5 and 2.3 in Python.framework (don’t ask what happened to 2.4).

```auto

<br />
$ ls -l /System/Library/Frameworks/Python.framework/Versions/<br />
total 8<br />
drwxr-xr-x 8 root wheel 272 3 Sep 18:33 2.3<br />
drwxr-xr-x 12 root wheel 408 6 Dec 23:58 2.5<br />
drwxr-xr-x 12 root wheel 408 6 Dec 23:58 2.6<br />
lrwxr-xr-x 1 root wheel 3 3 Sep 18:33 Current -> 2.6<br />

```

---

<div class="post-metadata">

**Author:** ![DJF3](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@DJF3](https://forums.plex.tv/u/DJF3)\
**Post date:** [January 12, 2010, 12:14pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/12 "2010-01-12T12:14:10Z")

</div>

Isn’t it possible to ‘save’ Applescript as an .app and run that from Plex?  
  
  
  
(I created an applescript that opens the Dashboard - containing webcams - that closes after 10 seconds)  
  
  
  
BTW, if someone has a working solution on how to run scripts/apps from a plugin, please add a message listing all steps you have to take to get it working (ideally with sample plugin file).  
  
  
  
DJ

---

<div class="post-metadata">

**Author:** ![oortmanp](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oortmanp](https://forums.plex.tv/u/oortmanp)\
**Post date:** [January 14, 2010, 9:31pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/13 "2010-01-14T21:31:46Z")

</div>

> [@](#):
>
> We target the Python 2.5 installation that ships with OS X. Although OS X 10.6 includes Python 2.6, it still includes 2.5 and 2.3 in Python.framework

  
  
I send HAS (developer of py-appscript) a mail regarding compiling py-appscript against 2.5. Here is his answer:  

> [@](#):
>
> Not familiar with Plex (do they not provide instructions for building modules/extensions?), but assuming it accepts Python .eggs, you could probably build against 10.6's default Python 2.5 install:  
>   
> sudo easy\_install-2.5 appscript  
>   
> then copy the resulting .egg from /Library/Python/2.5/site-packages  
>   
> If you need to build using Distutils alone (which appscript's setup.py will do if setuptools isn't installed), best ask on the general Python lists as I'm not sure how you force-bypass setuptools; it tends to get in everywhere.  
>   
> Note that if you build against a third-party Python framework, you may have problems unless said framework is built for the same architectures as the Plex one. (The Python 2.5 in 10.6 will be three-way build, which will probably cover your bases.)  
>   
> HTH  
>   
> has  
> --   
> Control AppleScriptable applications from Python, Ruby and ObjC:  
> http://appscript.sourceforge.net

  
  
So any idea if this .egg file could work when I include this in the library directory?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 8:28pm UTC](https://forums.plex.tv/t/can-you-run-applescript-within-a-plugin/2082/14 "2019-12-20T20:28:21Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
