0

I created an applescript code to help rename a bunch of images and create a text file with the file names and dimensions so we can send out to another team to program into an email.

The code worked perfectly fine when I was on Sierra, but ever since I updated to High-Sierra it does not rename all the files. Usually our emails probably have 4-20 images that need to be renamed, but with the new macOS the script will rename 2 images and stop with this message

**error "Can’t get item 1 of {}." number -1728 from item 1 of {}**

Here is the code, but the problem happens at

set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm

If anyone can help out I would appreciate it.

    --PopUp Window to select folder of files needing to be renamed
set iFolder to (choose folder with prompt "Select Folder")
--Sets folder path to readable location
set fPath to POSIX path of iFolder
--Sets folder path parent folder
set pPath to POSIX path of ((iFolder as text) & "::")
--removes listing invisible files
set fContent to list folder iFolder as list without invisibles
--seperates file name from extension
set text item delimiters to "."


tell application "Finder"
    --set gPath to fPath as string
    set all_files to every item of (iFolder) as list

    -- Multiple Selections
    set theDeviceList to {"Regular", "Luxury", "Holiday"}

    set theDevice to (choose from list theDeviceList with title "Device Selection" with prompt "Choose a Naming Convention:")

    set choices to the result as string
    -- Multiple Selections

    -- Begin Normal Email
    if choices is equal to "Regular" then
        -- Normal Email

        set new_FirstnameCol1 to "row"
        set new_LastNameCol1 to "-1col1image-responsive-image"

        set new_FirstnameCol2 to "row"
        set new_LastNameCol2 to "-2col2images-responsive2-image"

        set new_FirstnameCol3 to "row"
        set new_LastNameCol3 to "-3col3images-responsive2-image"

        --**Counter Begins
        --1 Column Counter
        set col1 to 1
        --2 Column Counter
        set col2 to 0
        --3 Column Counter
        set col3 to 0
        --Keeps count of all grouped columns
        set columnCounter to 0
        --Numbering convention to file name
        set universalCounter to 0
        set oopsCounter to 0
        --**Counter Ends

        repeat with i from 1 to the count of all_files
            --using our index, we select the appropriate file from our list
            set universalCounter to i - columnCounter - oopsCounter
            set this_file to item i of all_files
            set file_name_count to text items of (get name of this_file)
            set {itemName, itemExtension} to {name, name extension} of this_file

            set source_file to item i of fContent
            set fName to do shell script "basename " & quoted form of POSIX path of source_file
            set wholePath to fPath & source_file

            --Creates text file
            try
                do shell script "touch " & pPath & "results.txt"
            end try

            --Check to see if there is a file extension
            if itemExtension is "" then
                -- "" means there is no file-extension present.
                set file_extension to ""
            else
                --yup, we are currently processing a file that has a file-extension
                --we have to re-add the original file-extension after changing the name of the file!
                set file_extension to "." & itemExtension
            end if

            --Gets the height and width of each file
            set dimensions to paragraphs of (do shell script "sips -g pixelHeight -g pixelWidth " & quoted form of wholePath & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
            set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm
            set w to (item 2 of dimensions) --/ dpi_to_dpmm -- works out pixel width as mm


            if w is greater than or equal to 548 then
                --Col1
                set the name of this_file to new_FirstnameCol1 & universalCounter & new_LastNameCol1 & col1 & file_extension as string
                set txtName to new_FirstnameCol1 & universalCounter & new_LastNameCol1 & col1 as string


            else if w is greater than or equal to 288 then
                --Col2
                set col2 to col2 + 1
                set the name of this_file to new_FirstnameCol2 & universalCounter & new_LastNameCol2 & col2 & file_extension as string
                set txtName to new_FirstnameCol2 & universalCounter & new_LastNameCol2 & col2 as string
                if col2 > 1 then
                    set col2 to 0

                else
                    set columnCounter to columnCounter + 1
                end if
            else if w is greater than or equal to 101 then
                --Column 3
                set col3 to col3 + 1
                set the name of this_file to new_FirstnameCol3 & universalCounter & new_LastNameCol3 & col3 & file_extension as string
                set txtName to new_FirstnameCol3 & universalCounter & new_LastNameCol3 & col3 as string
                if col3 > 2 then
                    set col3 to 0

                else
                    set columnCounter to columnCounter + 1
                end if
            else if w is greater than or equal to 1 then
                --Oops
                set the name of this_file to new_FirstnameCol1 & new_LastNameCol1 & "_oops" & i & file_extension as string
                set txtName to new_FirstnameCol1 & new_LastNameCol1 & "_oops" & i as string

                set oopsCounter to oopsCounter + 1
            else
                --Oops
                set the name of this_file to new_FirstnameCol1 & new_LastNameCol1 & "_oops" & i & file_extension as string
                set txtName to new_FirstnameCol1 & new_LastNameCol1 & "_oops" & i as string

                set oopsCounter to oopsCounter + 1
            end if
            do shell script "echo '" & txtName & " " & w & " x " & h & "' >> " & pPath & "results.txt"

        end repeat
        --End Normal Email

        --** remove this section if adding more options
    else
        display alert "Something Went Wrong"
        --** remove this section if adding more options
        end if
end tell

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.