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

Feb 11, 2013 7:53 PM in response to red_menace

I have actually been doing a lot of that. I have also ran tests with that older code "4" on a plethera of differnt macs with new users. The strang this is that I was able to get it to work with a smaller file or I guess befor the shell script.... try throwing a big file into the drop. so crazy. I'm stil trying different things.

Mar 1, 2013 8:36 AM in response to mtimmons

hello,

you don't happen to know a great / quick way to have dropped folders support do you? I am doing some research for this now..I image in will need to go in the on open?


on opensomeItems-- Note that items dropped via CocoaAppletAppDelegate will be POSIX paths


# POSIX paths can be coerced by using something like the following


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


set itemCount to (countsomeItems) -- the number of items to process

try -- process items


initialize()


dispatch(someItems)

on error errmessnumbererrnum

if errnum is not -128 then display alert "Error: " & errnum message errmess

end try

tell me to quit -- done

end open


Mar 1, 2013 9:33 AM in response to mtimmons

Yes, the on open handler is what receives the items dropped onto the application. If you are talking about getting the contents of a folder instead of the folder itself, then you can go through the dropped items and if an item is a folder, replace it with the contained files - for example:


on open someItems -- dropped items will be POSIX paths      try -- process items           initialize()                      #expand folder contents           set myList to {}           tell application "System Events" to repeat with anItem in someItems                if ((class of disk item anItem) as text) is in {"folder", "«class cfol»"} then -- folder                     repeat with aFile in (get files of disk item anItem whose visible is true)                          set end of myList to POSIX path of aFile -- or coerce to alias, etc                     end repeat                else                     set the end of myList to anItem -- can also coerce to alias, etc                end if           end repeat           set itemCount to (count myList) -- the number of items to process                      dispatch(myList)      on error errmess number errnum           if errnum is not -128 then display alert "Error: " & errnum message errmess      end try      tell me to quit -- done end open

Mar 20, 2013 12:53 PM in response to red_menace

I was able to make your suggests along with some other work. Because the way my launcher app is designed, different functions need different ways to handle folders... anyways.


Have you every ouput a shell progress into your progres bar??


do shell script "rsync --progres " & file & " " & /path & return??


I have not done much research yet, but will be soon and figured you might have a quick example...

Mar 20, 2013 4:18 PM in response to mtimmons

Getting progress from rsync is a bit of a challenge, since even it doesn't know what it is going to do until it does it, and its progress option is more of a progress log. One way to get a more or less normal progress bar would be to do a dry run to get some statistics, then use NSTask and periodically check/parse the output pipe/file to syncronize the progress bar, but I'm thinking that might be a bit more work than you are looking for.

Mar 20, 2013 5:32 PM in response to red_menace

yeah I figured.. I am not neccessarily need to show a real progress. I am fine just using barbar pole and under neath showing the output.. revfreshing ever .5 s or so..


NewCreation_New_4ch.mov

226263040 59% 36.31MB/s 0:00:04


but.. so far I would need to tell the shell to curl to a file.. then have the progress bar get is data from that.. ......just like you said.

Apr 3, 2013 6:38 AM in response to mtimmons

so!! after a few weeks and a day off.. I was able to get something done here! After research I found ways to capture the PID of a shell script.. rsync here. and then I parse that to a text file.. then gather the percent info from that. SICK! EPIC SICK! I have also started reading Shane's book plan to slowly conver my little 'app' to a real cocoa interface.. someday.. 🙂 Please send me your critques to what I did below..



User uploaded file


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

set notificationTitle to "Copy to Desktop"

set theTargetFolder to (POSIX path of ((path to desktop folder)))

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

set percentageBefore to 0 -- resets progres percent info

if (progressController's stopProgress) as boolean then -- cancel?

set my progressMessage to "Canceling…"

exit repeat

else -- update the progress window for the current item

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)

set logOutput to POSIX path of ((path to temporary items from user domain) & "logout-" & (characters 3 thru end of ((random number) as string)) & ".txt" as string)




-- copy file

set thePID to do shell script "rsync --progress -a -h -u --exclude='.*' " & fposix & " " & theTargetFolder & " &> " & quoted form of logOutput & " & echo $!"


-- grab its ID

set processIsRunning to true


do shell script "sleep 1"



-- repeat while it's running

repeat until processIsRunning is false

tell application "System Events"

try


-- if this works, the process is still running

do shell script "ps -p " & thePID


-- so grab the last line of the output file


set progress to (last paragraph of (do shell script "cat " & (quoted form of logOutput)))



-- set percentage to word 3 of progress

set oD to AppleScript's text item delimiters

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


set percentage to (word 2 of progress)

set AppleScript's text item delimiters to oD



--get percentage value from sync output

try

set percentageNum to percentage as number


on error errStr number errnum

set percentageNum to 100

end try



--get percentage difference from last repeat

set percentageDif to ((percentageNum - percentageBefore))

set percentageBefore to percentageNum



-- tell progressController to setBarberPole_(true)

tell progressController--update progress by the perfect difference


updateProgressByAmount_topText_bottomText_(percentageDif, "Uploading File(s)", progress)

if its stopProgress then

my cleanup()

return

end if

end tell



on error


-- if we get here the process isn't running, so clean up

set processIsRunning to false

do shell script "rm " & quoted form of logOutput

try

do shell script "kill -9 " & thePID

end try

end try

end tell

end repeat

end repeat

end if


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

end testing

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.