How Mark Images in a Directory using Apple Scripts

dkpictureman

Registered
HI, Im found this apple script from a number of years ago and would like to develop it further. But am not sure how.

1) would like it to pop up and ask for name_list, instead of hard coding the image filename CMP_1750.jpg......
2) would like for it to request the directory to search not the currently active folder.
3) fix the script so that it does not take forever and a year to finish ( there is a bug I think)

the script should search for the name_list filenames in a selected directory and mark them all with an orange dot.
then pop up a dialog box to let the user know it is finished.

Any help would be appreciated!!!


set name_list to "CMP_1750.jpg
CMP_1754.jpg
CMP_1767.jpg
CMP_1772.jpg
CMP_1780.jpg" --just paste the whole text file into these quotes
set AppleScript's text item delimiters to ASCII character 10
if the number of text items of name_list < 2 then set AppleScript's text item delimiters to ASCII character 13 --depending on the alignment of the stars, the text above might be formatted differently but look exactly the same.
set name_list to every text item of name_list
tell application "Finder"
set the_folder to the folder of window 1 --the currently active folder in the Finder
repeat with n in name_list
try
set (the label index of every item of the_folder whose name begins with n) to 1 --label matches orange
end try
end repeat
end
tell


9541
 
Finder tell blocks in AppleScript are notoriously slow, but a Finder tell block is necessary to change the label of a Finder item. The most likely source of excruciating slowness in your script is this line in the Finder tell block loop:
set (the label index of every item of the_folder whose name begins with n) to 1
This line tells the Finder to look for every file which begins with the current value of n, so it looks for every file which starts with CMP_1750.jpg, then every file which starts with CMP_1754.jpg, and so on. Based on the attached image, there would be only one image file which would match any give image file name in name_list, so the search for matches is not necessary and will slow processing. The more files in a directory, the greater the slowdown. I ran your script with three image file names in name_list in a directory with over 500 images. It took several minutes for the loop in the Finder tell block to run. I ran the same three image file names for name_list in my script below and although I wasn't precisely timing it, the loop in the Finder tell block ran in approximately one second. Hopefully you will see similar results.

The script below assumes several things about how you want a script to work.
  • You are not trying to find matches for partial file names; i.e., name_list contains full image file names like CMP_1750.jpg, not something like CMP_17.
  • You want to paste the contents of name_list into a "pop up."
  • The contents of name_list are formatted with one file name per line with no extraneous spaces.
  • You can live with pasting name_list into the limited one line text box in an AppleScript dialog, aka "pop up." The dialog size and height of the text box cannot be specified in AppleScript. More than one line can be entered in a text box in an AppleScript dialog. There is no scroll bar, but up/down keys can be used to see one line at a time of the pasted text.

AppleScript:
-- Ask the user for name_list
set diagResult to display dialog "Paste name_list into the text box. Use up/down keys to see pasted text." default answer "" buttons {"ABORT", "OK"} default button {"OK"}

-- Process the output of the dialog
if diagResult's button returned is "OK" then
    -- No ASCII testing needed when using the AppleScript paragraph text element
    set name_list to every paragraph of (diagResult's text returned)
else if diagResult's button returned is "ABORT" then
    display alert "User aborted processing. Exiting..."
    error
end if

-- Ask the user to select the directory where images are located
set selFolder to choose folder with prompt "Select the directory containing the image files"

-- Set the label of image files in name_list to orange
tell application "Finder"
    repeat with n in name_list
        try
            set imgName to (selFolder as text) & n
            set the label index of (imgName as alias) to 1
        on error errMsg number errNum
            -- Processing is not stopped, error information is provided if one occurs; can comment out if you don't want to know
            if (errNum = -43) then
                display alert "Image file " & imgName & " was not found"
            else
                display alert "Some other error: errNum = " & errNum & linefeed & errMsg
            end if
        end try
    end repeat
end tell

If you want to format name_list differently or if any of my assumptions about what you wanted are wrong, post how you want it to work and I'll see what I can do.
 
Back
Top