Search code examples
macosautomationapplescriptautomatorfinder

Deselecting a file from a selection in Finder via AppleScript


I've been searching everywhere for around 6 hours to find a way to deselect file items from a previously selected array of items in Finder via AppleScript via Automator with a Quick Action. I've seen deselect all items, I've seen add items to a selection/list for process, but not remove items from a selection...

The goal: I usually have to process a lot of images and get them approved by supervisors before providing them on our servers for use. All of which come in a set of a transparent PNG and an opaque JPG. I only need to upload the JPG files over for approval. So, let's say I have 30 images. 15 of which are JPGs, 15 of which are PNGs. I would love to be able to select all 30 images, and deselect the 15 of which are PNGs (via the script), left with a selection of all the JPGs.

There are other ways to achieve this, but it would be exceptionally helpful if I could just select the images, run the script, and then move them over for upload (website upload). That appears to be the fastest (by hand) way to go about achieving the end result.

What I know: The selection property. Through my research, I've seen the selection property called in a small amount of different ways, but am unsure how to properly manipulate it. I've tried converting the selection to a string and splitting that by detecting a word to split from (like "Macintosh HD") allowing them to be manipulated in a list, and then potentially removing them from a selection...? I'm also very unsure how to just properly manipulate a property as-is...

I've also attempted to just use the given files via input through Automator AppleScript. But alas, that still leads to knowing how to remove/deselect a file from the current selection.

Anybody know how to achieve this?

I appreciate all the time spend and information in advance. Thank you.


Solution

  • There are two approaches you can use for this, one in pure AppleScript, the other using Automator actions.

    The pure AppleScript solution is take the selection as a list of files, copy the JPG files to another list, and set the selection to that new list, like so:

    set new_selection_list to {}
    tell application "Finder"
        set selection_list to selection
        repeat with this_item in selection_list
            if name extension of this_item is "jpg" then
                copy this_item to end of new_selection_list
            end if
        end repeat
        set selection to new_selection_list
    end tell
    

    Of course, if you're passing these files in through the input variable in automator, you'd use that rather than testing the selection again, but the principle still stands.

    Alternately, you can set up your workflow with a Filter Finder Items action, like so:

    enter image description here

    I'd recommend the second approach. It's likely significantly faster, and takes less programming.