Apple Event: May 7th at 7 am PT

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 27, 2013 3:21 PM in response to mtimmons

1. The path to resource command has an optional parameter that can be used to specify a subdirectory (the resource needs to be just the file name):


settrackCommandtoquoted formofPOSIX pathof (path to resource"prev_watch.sh"in directory"Scripts")


4. The ProgressBarController script in the bundle has a few different routines to initialize and show the progress window so it can be set up in different ways. You could set it up initially to be a barber pole, or set everything up and then show it, etc, but I was trying to get everything into a common initialization handler - I've moved the initial showing of the progress window to after any other dialogs. Another thing I changed was getting the name of the file items - System Events can use either POSIX or HFS paths, text or alias by doing something like


tellapplication"System Events"tosettheNametonameofdisk item (anItemastext)


5. The .xib files are compiled and don't have much to navigate by so I don't know about removing entire menus, but by searching for the text "cocoapplet" you can find the application name in the menu items and replace it.


I've updated my example of your project here.

Jan 27, 2013 6:51 PM in response to mtimmons

I changed the bundle name in the info.plst and was able to get the "quit..." menubar to be right. Also since I am using a shell cp command for copies right now, I changed the progress to barberpoll very easily thanks to your great documentation. Terminal in this setup does not give us a true porgress, so a simple barberpoll worked percfect until I change this behavior. I am very happy with my lasted build and am very thank full for all your help! I have learned a lot.

Feb 9, 2013 9:50 AM in response to red_menace

hello..

I noticed that the "Cancel" does not work with this new code? Is that the same for you too? here is a block. Let me know if you want the full script again.


on testing(inputObjects) -- test by copying items to the desktop

set notificationTitle to "Copy to Desktop"

tell progressController to showProgressWithTopText_bottomText_maxValue_("Testing…", "", (itemCount * 100) + 1)

tell current application's NSThread to sleepForTimeInterval_(delayTime)


repeat with anItem in inputObjects

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

tell application "System Events" to set theName to name of disk item (anItem as text)

tell progressController


updateProgressByAmount_topText_bottomText_(100, "Testing…", theName)

if its stopProgress then

my cleanup()

return

end if

end tell

tell current application's NSThread to sleepForTimeInterval_(delayTime)


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

set allNames to allNames & return & theName

end repeat

notify(notificationTitle, "Files copied to Desktop.", allNames)

end testing

Feb 10, 2013 4:45 PM in response to red_menace

interesting. I have tried few diffent build and nothing once the shell script starts.. with or without the relative path, which had not showed any problems yet. Perhaps you could shoot me a link to the build you confirmed it on? maybe something crazy is happening. note taken on the tilde.. I may recode for 'active user' instead.

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.