How to count the number of files in selection with Applescript

Hello, Im trying to work in a repeat condition with the number of times repeated is 1 less than the number of pdf files selected in finder and Im wondering how to code that scenario. Im new to applescripting and cant get a proper integer result from trying to count the number of items i currently have selected.


Any help would be greatly appreciated

Posted on Jun 14, 2014 5:09 PM

Reply
10 replies

Jun 14, 2014 6:08 PM in response to rshughes205

The code that I started putting together at work is an automtor application with a run-applescript which does a save as pdf of all open .musx files in finale 2014 (music notation program). It kinda goes like this:


automator - [create folder "pdf export"]

automator - [get specifed finder items]

automator - [open finder items with Finale 2014]


AppleScript - [keystroke/print dialog commands to save the front document as a pdf in the "pdf export folder" on the desktop, then closes the file]


The probelm im having is a repeat condition - if i run the apple script in the automator it does it once, and I need it to do it to all the open files. If i repeat 200 times, but only brought in 30 finale files, its going to keep trying to repeat the script without an open file to do it to and won't continue in any line of code or in automator.


What I was thiinking is if I could "activate" finder, count the number of selected files that I brought into the Automator application, I could set that value-1 as the number of times to repeat the "save as pdf" run-applescript, and could continue with other applescript or automator commmands once the sequence has finished.


I guess the other option is a "repeat while" or "repeat until" command, but I dont know what that condition would be. At the end I should be left with Finale 2014 running wtih no more open documents and i'd like the sequence to finish.


idk of thats a


"repeat while front document of Finale 2014 exists"


sort of thing or what.


This is my 3rd day of applescripting but if I set the repeat x times to the number of files I bring into the automator/applescript, it works, which I'd say is decent progress, but I will be bringing varying numbers of files into that automator application quite often so i can't leave the repeat condition at a specified number.

Jun 14, 2014 6:32 PM in response to red_menace

This is kinda what my automator file that I left at work looks like (except its Finale 2014) In this setup, are all of the files in "get specified" input into the run applescript action?

User uploaded file

When I first ran everything together it only ran the applescript save-as-pdf thing once, and after it closed the first file it didn't do it to the next open document and your saying if i add this:


repeatwithanItemintheinput


# do something with anItem, which is the current item from the list

endrepeat


It will repeat for all of those open documents? That might be it. As soon as I can get my hands on the AS code i'll post it.

The "print finder items" prints all finder items but the group of guys tasked with figuring this out can't seem to use that to save as pdf for all of the finder items, it just sends them to our printers as it seems to skip the OS print dialog in Finale 2014, which is why im trying to do it with apple script.

Jun 14, 2014 6:41 PM in response to rshughes205

The Run AppleScript action is passed the results from all of the previous actions in the input variable, so to handle those you would just repeat through the list contained in the variable. I'm beginning to wonder if your workflow only works with a single item at a time though, so in that case you can download the Dispense Items Incrementally action, which will take an original list of items and loop through them one at a time.

Jun 14, 2014 6:57 PM in response to red_menace

I'm not sure what the difference between a workflow and an application is, but I currently am wanting to use it as an application bound to the finder window where I can drag files onto the app-button and have it run through this. The actuall applsecript is all this kind of stuff:

-------------

keystroke "p" using command down


click menu button "pdf" of sheet of window "print"


click menu item "save as pdf" of sheet of window "print"


keystroke "d" using {command down, shift}


keystroke "g" using {command down, shift}


type value of text field 1 "PDF export"


key code 76


repeat until exists sheet of window "print"

end repeat


key code 76


etc.

----------

Some of that is very wrong, but I can't remember what working code I found/copied and amended for my scenario/names/filepaths. None of what I have in the Applescript works off of an input at the moment. Would I have to define an input?


and, if i use the Dispense Items Incrementally action, with that repeat all the actions in the automater per file and try to create a new folder called "pdf export" each time?

Jun 14, 2014 7:14 PM in response to rshughes205

An Automator workflow is just a series of actions that perform your task. It can be saved as an application, in which case items to be passed to the workflow can be droped onto the application icon.


If you aren't doing anything with the list of items passed to the Run AppleScript action, then that sounds like you are just using the current document of the application you are using those commands with. This may be a part of the problem, since you would need to make sure that the current document gets loaded and saved correctly. The Dispense Items Incrementally action would help in that situation - it works with the Loop action, to loop through the input items one at a time.


The New Folder action will just pass on the path of the folder if it already exists - note that if you are not going to be using the results of a previous action, you should look into using the Ignore Input option to keep from adding undesired items into your workflow, for example adding the path of your PDF folder to the list of specified Finder Items.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to count the number of files in selection with Applescript

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