Applescript to run a shell script?

Discussion in 'Mac Programming' started by jeof, Apr 18, 2012.

  1. jeof macrumors newbie

    Apr 18, 2012
    I used Automator to create a shell script that executes a terminal command to bundle a .war file.

    I have it saved as a file called: make_war_file

    Is there an easy way i can create a script that does this:

    Wait until this folder appears: /output/folder_name

    When it does, run the shell script called: make_war_file


    If waiting for a folder is problematic for some reason, i can tell it the name of a file inside of it instead.

  2. jeof thread starter macrumors newbie

    Apr 18, 2012

    Sorry that was such a newbie question, but in case there are others reading this , the solution I found was to just set up an Automator "Folder Action" on the "output" folder.

    I was trying to do it as a "workflow" application but that seems to require manually running the application, whereas a folder action acts by itself whenever the folder changes.

    The folder action is set on the "output" folder, and starts with "Filter files" that acts when a folder named "folder_name" is added, followed by a "Run shell script" that contains this text:

    cd /output/folder_name
    jar cf help.war *

    So now whenever Oxygen XML editor generates output and deposits "folder_name" inside of the folder named "output", the shell navigates to inside the folder_name folder and runs the jar command to bundle it into a web application called help.war.

    It only seems to work if "folder_name" didn't exist before it's written, so now I'll try to figure out how to specify that it act when a new version of the folder is written, overwriting the old one. If not I just have to delete the old one each time, which isn't a terrible chore.
  3. kryten2 macrumors 6502a

    Mar 17, 2012
    If your folder_name always has the same name that would be logical. You can't have 2 folders with the same name.

    Attach this script to your output folder :

    on adding folder items to this_folder after receiving these_items
    	-- insert actions here
    		do shell script "cd " & quoted form of POSIX path of these_items & ";" & "jar cf help.war *"
    		--delay 5 -- uncomment to wait 5 seconds if the creation of the web archive takes some time
    		do shell script "cp " & quoted form of POSIX path of these_items & "help.war" & space & POSIX path of (path to desktop)
    		delay 1
    		tell application "Finder" to delete these_items
    	on error the error_message number the error_number
    		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
    	end try
    end adding folder items to
    The script will create the web application, copy the help.war to your desktop(I do this because you create it to do something with it, don't you?).
    Then the added folder is moved to the trash. The script only processes one folder, add a repeat loop if you want to process more than one folder.
  4. jeof thread starter macrumors newbie

    Apr 18, 2012
    No I meant it I couldn't get it to overwrite the old one. if I put a folder with the same name in manually, that's what it would do, after asking me if i wanted to replace. It wouldn't create two folders with the same name.

    I finally realized that I could just extend the workflow to move the folder, similar to what you suggest, after the first process was done. Discovering that you can "pause" in Automator was useful.

    I'll try yours though, might be better. Thanks, appreciate it.

Share This Page