Applescript & Automator Help

Discussion in 'Mac Programming' started by rfitz9, Jan 10, 2016.

  1. rfitz9 macrumors newbie

    rfitz9

    Joined:
    Jan 10, 2016
    Location:
    Colorado
    #1
    Hello All,

    I'm new to Applescript and Automator. After reading this post, I've become obsessed with scripting and automating. I'm basically trying to teach myself - and having a lot of fun doing so. I'm trying to figure out how to script a task and I'm getting stuck. Here's what I'm trying to do:

    1. Check finder to see if an external disk is mounted.
    2. If disk is mounted, launch Plex.
    3. If disk is not found, do nothing.

    I'm using a Macbook Pro, so I don't always have the external drive connected. I would love to have a script running that checks finder every X seconds. If it locates the drive, then it launches Plex. Or else it does nothing. Is this task possible with Automator or do I need to write a script?

    Any help would be greatly appreciated.
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    Automator doesn't have any conditional operations, so you would need to use a script. Instead of constantly polling with the Finder, you can set up a folder action on the /Volumes folder - this is a normally invisible folder that contains aliases to mounted volumes. The folder action would be launched by the system whenever an item is added to the folder (when the drive is mounted), so the script would just check if the items passed to the folder action contain one you are looking for, starting your application if it is there.
     
  3. rfitz9 thread starter macrumors newbie

    rfitz9

    Joined:
    Jan 10, 2016
    Location:
    Colorado
    #3
    Since I'm new to this, can you tell me if this is correct?

    Code:
    on adding folder items to Volumes after receiving <Name of External HD>
        tell application "Finder"
            if name of <Path of External HD> is "Ryan's EHD" then
                open application "Plex Media Server"
            end if
        end tell
    end adding folder items to
     
  4. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    The adding folder items handler has the folder that was added to (since the script can be attached to multiple folders) and the items added as parameters (the added items will be a list, even if there is only one item).

    The way you attach the folder item script is to right-click on a folder and select Folder Action Setup from the Services sub-menu (since the /Volumes folder is normally invisible, you can open it in a Finder window by using the Go To Folder menu item). From the setup dialog, enable folder items and add your script from the popup - your script will need to be in the Scripts/Folder Action Scripts folder in either the local or your user's Library folder to show up in the list (you can make the folders if they aren't already there).

    Code:
    on adding folder items to this_folder after receiving these_items
        tell application "Finder"
            if name of first item of these_items is "Ryan's EHD" then -- just get the first item
                tell application "Plex Media Server" to activate -- launch, run, and make app frontmost
            end if
        end tell
    end adding folder items to
     
  5. rfitz9, Jan 12, 2016
    Last edited: Jan 12, 2016

    rfitz9 thread starter macrumors newbie

    rfitz9

    Joined:
    Jan 10, 2016
    Location:
    Colorado
    #5
    I'm sorry for all the questions, but I just don't get how I would be able to right-click on the /Volumes folder. If I use Go To Folder it just opens /Volumes, but doesn't allow me to right click the folder to do the Folder Action Setup.

    EDIT: Spoke too soon. Figured it out and it works great. Thanks so much for your help!
     

Share This Page