Previous 1 2 3 Next 38 Replies Latest reply: Apr 3, 2013 6:38 AM by mtimmons
mtimmons Level 1 (0 points)

I have a nice little applescript (on run.. inputObects) that presents a list of actions to perform on the files dropped. One of these actions is a simple copy using cp in the shell. Everything works great, execept, I need a way to cancel the script. yes, I could do a file > Quit on the scrip App. But, for my users, that is not convenient. I like how automater puts a 'cog' in th menubar that you can 'cancel' a script with. However, my applescript is too complicated for automator as the main app because of resources and other things I am doing.


I tried creating a 'dummy' automater script that delays indefinently but has an 'on quit' hander quitting the main applscript app. However, when choosing to 'cancel the workflow' from the cog, it does not run the worflows 'on quit' handler.. and thus does not solve my problem.


That said, I desire to change to bavior for the applscript to open an automater app that does the actions instead of nesting them in my main script....Unless there is a better way to implement a "cancel" of Applscript while it is doing 'stuff'.


So, does anyone know how to send the same input from the dropped-on file into the automator's inputs? and then get the return?


here is a code scketch.. very simplified. (applescript read-only app):


on open of inputObjects

          set workerPath to quoted form of POSIX path of (path to resource "" in directory "Scripts")

  do shell script "open " & workerPath & inputObjects


  -- now, get the output form the automator script..

  display dialog allNames


end open



Automator, applescript (


on open of inputObjects

          repeat with i in inputObjects

                    set fileCopies to fileCopies & inputObjects

                    set fposix to the quoted form of the POSIX path of i

                    do shell script "cp -f " & fposix & " ~/Desktop/"

  --- GET ALL THE FILENAME(S) of files dropped

                    set oD to AppleScript's text item delimiters

                    set AppleScript's text item delimiters to {"/"}

                    set fileName to (last text item of fposix)

                    set AppleScript's text item delimiters to oD

                    set AppleScript's text item delimiters to "."

                    if number of text items of fileName > 1 then

                              set finalName to text items 1 thru -2 of fileName as text

                    end if

                    set allNames to allNames & "\n" & finalName


          end repeat

          return allNames

end open

  • red_menace Level 6 (15,000 points)

    I haven't had that much luck with the Automator running menu - what OS are you running, and how are you creating your AppleScript?  AppleScript in Xcode or a Cocoa-AppleScript can put up non-modal dialogs that can include a cancel button.

  • mtimmons Level 1 (0 points)

    It is done in regular Applescript edtir.

    all the dialogs I know of like "display digalog 'blah' with buttons....." but holds the script until you clear the box. Ideally, I would have a dialog box with 'Cancel' WHILE the script is running.

  • red_menace Level 6 (15,000 points)

    If you are using Lion or Mountain Lion, a Cocoa-AppleScript can be used.  I have a Cocoa-AppleScript applet template for a progress window that has a cancel button - the example just steps through the dropped items without doing anything with them, but you can drop your code into it.  The template can be downloaded here.

  • mtimmons Level 1 (0 points)

    wow. wonderful. You say lion / mt lion. I just opened this and tested also in 10.6.8 (I have to deploy also to that) and it worked fine??


    I will try to implement this.. as long as you don't mind me changing the icon and such.


    The other rediculous thing I thought of was to launch another app that if you canceled it... then it would quit the main app.. Or  closing the "another app" when the script finishes.

  • red_menace Level 6 (15,000 points)

    The Cocoa-AppleScript application will run in Snow Leopard, but I'm not sure how the Script Editor will handle editing it in Snow Leopard, since additional Cocoa stuff was added to the AppleScript Editor in Lion.  Since the applet knows when you press the cancel button (or when it finishes), you can do whatever you need.

  • mtimmons Level 1 (0 points)

    Cool, I am editing in 10.8, so all good. I looked at yor code at place my shellscript stuff here:


      # do something with the item - not doing anything in this example, so just delay a bit

                                  tell current application's NSThread to sleepForTimeInterval_(0) 


                                  set fposix to the quoted form of the POSIX path of anItem

                                  do shell script "cp -f " & fposix & " ~/Desktop/"



    Everything worked. However, I need to run this with 'progress bar code' with several different job options. How would you suggest doing this beside copy and pasting the fool out of it. I could write an "if selectedAction is blah1 then ... else... blah2....." and put it where the other code is. Or, is there a way to wrap the set of code I need to execture in anothrer handler? Thanks for your help!


    In case your are curious, my orignal (no progress bar) applescript code is here:

  • mtimmons Level 1 (0 points)

    just solved my own problem..

    at the top after I select my action I used:



    # do something with the items

                                  set someItems to prepareItems(someItems)








    during the processStuff hanldeer I put:



    # do something with the item - not doing anything in this example, so just delay a bit

                                  tell current application's NSThread to sleepForTimeInterval_(0)



    end processStuff



    when you are setting up your hanlders I put:


    on ActionDesktop(anItem)


              set fposix to the quoted form of the POSIX path of anItem

              do shell script "cp -f " & fposix & " ~/Desktop/"


    end ActionDesktop


    on cleanup() -- clean up before quitting

  • red_menace Level 6 (15,000 points)

    Breaking up your script into several smaller handlers makes things easier to work with and debug.  For example, you could put each function in its own handler (which allows for adding and changing functions), and put common tasks such as notifications into their own handlers (note that you can use Mountain Lion's Notification Center directly).


    My Progress Window template has a class in the bundle that provides various handlers/methods for common things such as changing the text fields, and these handlers are what are being called from the example script.  The class is commented fairly well (although I always welcome feedback), so you can take a look to see what is available - the example application shows how to use some of the methods, but you are free to rearrange oe replace things as desired.


    When initializing the application, you could create the progress window instance and place it into a property where it will be available in all of your handlers, then put up your function dialog.  The selected function would then be dispatched by calling the appropriate handler, where you would set up the various progress window parameters, and show the progress while processing the items.  The progress window instance can be reused by closing the window, resetting items as desired, and showing the window again.

  • mtimmons Level 1 (0 points)

    I spent a good amount of time last night analyzing the code and trying to implement the applscript into it...

    simple processes I had setup work fine.. like what I tested. However, I have on batch re-namer that runs before any progress bar code or functions and it no longer likes how I am getting the file inputs.


    current code:


    else if selectedAction = "Batch Rename" then


                        set theName to the text returned of (display dialog "Enter Base File Name" default answer "")

                        set counter to "1"

                        set numberOfFiles to count of someItems

                        tell application "Finder"

                                  set digits to ((length of (numberOfFiles as text)) + 1)

                                  repeat with i in someItems

                                            set itemExtension to name extension of i

                                            if itemExtension is not "" then set itemExtension to "." & itemExtension

                                            set counter to text -digits thru -1 of ("00000000000000" & (counter))

                                            set the name of i to (theName & "_" & counter & itemExtension)

                                            set counter to text -digits thru -1 of ("00000000000000" & (counter + 1))


                                  end repeat

      display dialog "Rename Complete!" buttons {"OK"} default button 1 with icon path to resource "droplet.icns" in bundle (path to me)


                        end tell



              end if



    I placed all the progress bar functions into one big function  "progressBarApplication(someItems)" That I can then call when I need to only on certain opperations of the script.. but the renamer will not work even withouth this function. perhaps it is the way the cocoa script works vs a basic apple script?

  • red_menace Level 6 (15,000 points)

    I was also playing with your script, and the renamer part seems to work OK for me.  Errors in a Cocoa-AppleScript application are handled a bit differently than regular AppleScript, in that an error just causes the containing handler to be aborted (another reason to break up the script) - the application normally doesn't quit.  In your renamer section, the use of path to me in your dialog will not work, although it is not needed (the path to recource uses the current bundle).

  • mtimmons Level 1 (0 points)

    In my original script, everything works fine.

    However, on implementaion of the re-namer tool, it errors. I pulled out the code for the icon resource all together (i feel like I had to use (path to me) b/c it did not work in 10.6.8?? not sure... been working on this 3wks + now)


    It just seems the error is on:

    repeat with i in someItems


    On the "i" part.

    Screen Shot 2013-01-27 at 11.38.20 AM.png


    may I ask how you implemeneted it to work in your applescript? I know I need to do some optimizaion.. but I stuck my head in deep on this project and am still learning a lot as you can tell.


    also - on notification center. I am using terminal-notifier since I could use my own icon.. direct calls originally with applescript were using something else I believe..but that was last week when I implemented the notifier. Also, I am using some bash scripting on some 'watch' procesess and it was just easier for me to deal with at the time of my coding level.

  • red_menace Level 6 (15,000 points)

    That error looks like you are trying to get the name of a text string, so make sure the statement is being targeted to the right application.


    I rearranged the code from your original project to make it easier to work on, and added statements to use Mountain Lion's Notification Center.  I don't have the remote volumes, of course, but the test and batch rename parts are working - I uploaded the example application here if you want to take a look at it.

  • mtimmons Level 1 (0 points)

    amazing.. looking into it now. This will be very helpful in my learning.

    fyi, I see batch rename working, but not on the "drop on" of files. which is how I will use it... It work when you open the app, but not on drop on. Do you get an error with that?

  • red_menace Level 6 (15,000 points)

    OK, I see what it is - the items dropped onto the application are sent as POSIX paths.  My original template didn't use the Finder or System Events to get the name, so I'll be updating my templates.  In the mean time, you can coerce the dropped items to aliases if they are text (the POSIX paths) - just add the following to the beginning of the open handler (someItems is the parameter name I used with my open handler):


      repeat with anItem in someItems -- coerce to alias in place (if needed)

        if class of anItem is text then set contents of anItem to (anItem as POSIX file) as text as alias

      end repeat

Previous 1 2 3 Next