Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript Input into Automator input...

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.


mainApp.app (applescript read-only app):


on open of inputObjects

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


do shell script "open " & workerPath & inputObjects



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


display dialogallNames


end open



Automator, applescript (Worker.app):


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

Posted on Jan 26, 2013 1:01 PM

Reply
38 replies

Jan 26, 2013 2:04 PM in response to red_menace

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.

Jan 26, 2013 5:00 PM in response to red_menace

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:

http://fcpost.com/dl.php?file=files/support/FCLauncher-CODE.zip

Jan 26, 2013 5:17 PM in response to mtimmons

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)


processStuff(someItems)



cleanup()

...




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)


ActionDesktop(anItem)


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

Jan 26, 2013 5:37 PM in response to mtimmons

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.

Jan 27, 2013 7:46 AM in response to red_menace

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 iconpath 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?

Jan 27, 2013 8:53 AM in response to mtimmons

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).

Jan 27, 2013 9:50 AM in response to red_menace

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.

User uploaded file


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.

Jan 27, 2013 10:28 AM in response to mtimmons

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.

Jan 27, 2013 11:46 AM in response to mtimmons

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):



repeatwithanIteminsomeItems-- coerce to alias in place (if needed)

ifclassofanItemistextthensetcontentsofanItemto (anItemasPOSIX file) astextasalias


endrepeat

Jan 27, 2013 2:42 PM in response to red_menace

debugging and stuff is going good. a few notes.


1. Just tested on 10.6.8 and it can't find my script resources and errors out.

....

set trackCommand to quoted form of POSIX path of (path to resource "./Scripts/prev_watch.sh")

....

I have messed with /Scripts/ and just 'Scripts/' but nothing. I guess I will retry the (path to me..) bundle? I am certain that is why I did that before. 10.7 and 10.6.8 did not like it.


2. One one action , my shell script watches "in the background" for the encode (appended filename in a specific folder) to complete and then fires the notification, so I still needed that.. but it is working perfectly after I created a new "theNameOnly" without the filename in the extension.


3. On the renamer, I like the mechanism I had to where it would always add just one leading '0' to the digit count as opposed to a bunch of 0000. I was able to adopt your code perfectly.


4. something I tried and failed at was moving the initialization of the progressbar on several actions to after the 'choose folder'.. I found it odd to see a progress bar before you told it to do something. Not a big deal though. This is almost done and I have learned a ton !


4. Lastly, not a necessity, but a request since I can't edit the NIB. would be to remove the unnecessaries (Edit, format...) from the menu bar. And also change the Application > Quit "CocoaApplet" to the bundle name. Not a biggie, but an idea for future users.


you can look at my update here.

Applescript Input into Automator input...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.