# Running an Applescript in the background

**URL:** <https://forums.plex.tv/t/running-an-applescript-in-the-background/9760>\
**Category:** Dev/API Corner\
**Tags:** other-dev\
**Created:** [June 25, 2011, 9:13pm UTC](https://forums.plex.tv/t/running-an-applescript-in-the-background/9760 "2011-06-25T21:13:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![benpackard](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@benpackard](https://forums.plex.tv/u/benpackard)\
**Post date:** [June 25, 2011, 9:13pm UTC](https://forums.plex.tv/t/running-an-applescript-in-the-background/9760/1 "2011-06-25T21:13:12Z")

</div>

Hi all  
  
  
  
I wrote a small applescript that connects/disconnects my VPN connection. I saved it as an app and added it to Plex. It works quite well on the whole, and I’m happy to share it if anyone is interested.  
  
  
  
My next goal is to run this less disruptively. I added growl notifications to provide success feedback while plex is full screen, but of course Plex automatically closes and re-launches once the script completes.  
  
  
  
Is there any way to stop plex from closing when the app is launched?

---

<div class="post-metadata">

**Author:** ![anasazi](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/anasazi/32/287_2.png) [@anasazi](https://forums.plex.tv/u/anasazi)\
**Post date:** [June 26, 2011, 5:27pm UTC](https://forums.plex.tv/t/running-an-applescript-in-the-background/9760/2 "2011-06-26T17:27:22Z")

</div>

Save this applescript as an application:  
  
  
  
tell application “System Events”  
  
tell current location of network preferences  
  
set vpnStrong to service “ActivateVPN” – name of VPN  
  
set isConnected to connected of current configuration of vpnStrong  
  
  
  
if isConnected then  
  
if exists vpnStrong then disconnect vpnStrong  
  
– Check if Growl is running:  
  
tell application “System Events”  
  
set isRunning to ¬  
  
(count of ¬  
  
(every process whose name is “GrowlHelperApp”)) \> 0  
  
end tell  
  
  
  
–Only display notifications if Growl is running:  
  
if isRunning = true then  
  
  
  
tell application “GrowlHelperApp”  
  
– Make a list of all the notification types   
  
– that this script will ever send:  
  
set the allNotificationsList to ¬  
  
{“Ansluten”, “Frånkopplad”}  
  
  
  
– Make a list of the notifications   
  
– that will be enabled by default.   
  
– Those not enabled by default can be enabled later   
  
– in the ‘Applications’ tab of the growl prefpane.  
  
set the enabledNotificationsList to ¬  
  
{“Ansluten”, “Frånkopplad”}  
  
  
  
– Register our script with growl.  
  
– You can optionally (as here) set a default icon   
  
– for this script’s notifications.  
  
register as application ¬  
  
“Växla VPN” all notifications allNotificationsList ¬  
  
default notifications enabledNotificationsList ¬  
  
icon of application “System Preferences”  
  
  
  
– Send a Notification…  
  
  
  
notify with name ¬  
  
“Frånkopplad” title ¬  
  
“VPN” description ¬  
  
“Frånkopplad VPN” application name “Växla VPN”  
  
  
  
end tell  
  
end if  
  
else  
  
if exists vpnStrong then connect vpnStrong  
  
– Check if Growl is running:  
  
tell application “System Events”  
  
set isRunning to ¬  
  
(count of ¬  
  
(every process whose name is “GrowlHelperApp”)) \> 0  
  
end tell  
  
  
  
–Only display notifications if Growl is running:  
  
if isRunning = true then  
  
  
  
tell application “GrowlHelperApp”  
  
– Make a list of all the notification types   
  
– that this script will ever send:  
  
set the allNotificationsList to ¬  
  
{“Ansluten”, “Frånkopplad”}  
  
  
  
– Make a list of the notifications   
  
– that will be enabled by default.   
  
– Those not enabled by default can be enabled later   
  
– in the ‘Applications’ tab of the growl prefpane.  
  
set the enabledNotificationsList to ¬  
  
{“Ansluten”, “Frånkopplad”}  
  
  
  
– Register our script with growl.  
  
– You can optionally (as here) set a default icon   
  
– for this script’s notifications.  
  
register as application ¬  
  
“Växla VPN” all notifications allNotificationsList ¬  
  
default notifications enabledNotificationsList ¬  
  
icon of application “System Preferences”  
  
  
  
– Send a Notification…  
  
notify with name ¬  
  
“Ansluten” title ¬  
  
“VPN” description ¬  
  
“Ansluten till VPN” application name “Växla VPN”  
  
  
  
end tell  
  
end if  
  
end if  
  
end tell  
  
end telll  
  
  
  
  
  
  
  
Then add it to your application list in Plex or add this line to your Harmony keymap if you using that:  
  
  
  
Plex.RunAppleScript(tell application “ActivateVPN” to activate)  
  
  
  
Make sure that Growl has the option “launch at startup” checked because sometimes the notification didn’t work if this option wasn’t enabled.

---

<div class="post-metadata">

**Author:** ![benpackard](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@benpackard](https://forums.plex.tv/u/benpackard)\
**Post date:** [June 26, 2011, 5:40pm UTC](https://forums.plex.tv/t/running-an-applescript-in-the-background/9760/3 "2011-06-26T17:40:35Z")

</div>

Just to be clear - I already wrote a similar application and am launching it in the same manner. I am asking if there is a way to run it without it closing/re-opening Plex.

---

<div class="post-metadata">

**Author:** ![anasazi](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/anasazi/32/287_2.png) [@anasazi](https://forums.plex.tv/u/anasazi)\
**Post date:** [June 26, 2011, 6:03pm UTC](https://forums.plex.tv/t/running-an-applescript-in-the-background/9760/4 "2011-06-26T18:03:03Z")

</div>

> [@](#):
>
> Just to be clear - I already wrote a similar application and am launching it in the same manner. I am asking if there is a way to run it without it closing/re-opening Plex.

  
  
Sorry, my bad...  
Are you using some kind of remote eg. Harmony?

---

<div class="post-metadata">

**Author:** ![benpackard](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@benpackard](https://forums.plex.tv/u/benpackard)\
**Post date:** [June 26, 2011, 6:36pm UTC](https://forums.plex.tv/t/running-an-applescript-in-the-background/9760/5 "2011-06-26T18:36:14Z")

</div>

No - most frequently I use my bluetooth keyboard or use teleport from my macbook. I do have a keyboard shortcut set up, but it isn’t picked up when plex is running full screen.

---

<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, 9:10pm UTC](https://forums.plex.tv/t/running-an-applescript-in-the-background/9760/6 "2019-12-20T21:10:46Z")

</div>

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