Thursday, August 11, 2022

#1 2022-06-05 06:53:18 am

Stadler
Member
Registered: 2008-07-09
Posts: 14

separate images into folder horizontally and vertically?

Hi,
I've used this script before without issues (Prior to macOS Catalina), now I am getting " error "Can’t get dimensions of missing value." number -1728 from «class dmns» of missing value "
How can this be corrected?

Thanks for your time and experience


tell application "Finder"
    set theFolder to choose folder
    if not (exists folder "Portrait" in folder theFolder) then
        make new folder in folder theFolder with properties {name:"Portrait"}
    end if
    if not (exists folder "Landscape" in folder theFolder) then
        make new folder in folder theFolder with properties {name:"Landscape"}
    end if
    set fileList to document files of theFolder
end tell
tell application "Image Events"
    launch
   
    repeat with theFile in fileList
        set theImage to open theFile as alias
        copy the dimensions of theImage to {Horiz, Vert}
        close theImage
        if Horiz is greater than Vert then
            -- landscape
            tell application "Finder"
                move theFile to folder "Landscape" of folder theFolder
            end tell
        else
            -- portrait
            tell application "Finder"
                move theFile to folder "Portrait" of folder theFolder
            end tell
        end if
    end repeat
end tell

Offline

 

#2 2022-06-05 07:47:57 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1403

Re: separate images into folder horizontally and vertically?

I've included my suggestion below. I tested the script on Monterey and it worked as expected.

Applescript:

set theExtensions to {"png", "tiff"} -- edit as desired but digital photos may not sort correctly
set theFolder to (choose folder) as text
set landscapeFolder to (theFolder & "Landscape:")
set portraitFolder to (theFolder & "Portrait:")

tell application "Finder"
   if not (exists folder portraitFolder) then make new folder in folder theFolder with properties {name:"Portrait"}
   if not (exists folder landscapeFolder) then make new folder in folder theFolder with properties {name:"Landscape"}
   set fileList to (every file in folder theFolder whose name extension is in theExtensions) as alias list
end tell

tell application "Image Events"
   launch
   repeat with theFile in fileList
       set theImage to open theFile
       set {Horiz, Vert} to dimensions of theImage
       close theImage
       if Horiz is greater than Vert then
           my moveFile(theFile, landscapeFolder)
       else
           my moveFile(theFile, portraitFolder)
       end if
   end repeat
end tell

on moveFile(theFile, theFolder)
   try
       tell application "Finder" to move theFile to folder theFolder
   on error
       tell application "Finder" to set fileName to name of theFile
       display alert "A file already exists in the target folder" message fileName buttons {"Skip"}
   end try
end moveFile

Last edited by peavine (2022-06-06 10:06:57 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#3 2022-06-15 02:49:45 pm

Stadler
Member
Registered: 2008-07-09
Posts: 14

Re: separate images into folder horizontally and vertically?

SAAWEEET!  Thank you soooo much!!  You saved my hide!

Offline

 

#4 2022-07-05 10:08:19 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1403

Re: separate images into folder horizontally and vertically?

The script included above sorts and moves image files into portrait and landscape folders based on their relative width and height. If the images are digital photos, this may not be a reliable approach, and the following script uses metadata orientation instead. It should be noted that when photos are placed on an external drive, there is a delay in indexing the files, and the following script takes no action if run before indexing is complete. A workaround is to sort the photos on the boot drive. Please test this script with copies of photos until satisfied that the script works as desired.

Applescript:

on main()
   set theFolder to (choose folder)
   set landscapeFolder to (theFolder as text) & "Landscape:"
   set portraitFolder to (theFolder as text) & "Portrait:"
   makeFolders(theFolder, landscapeFolder, portraitFolder)
   set landscapeFiles to getFiles(theFolder, 0)
   set portraitFiles to getFiles(theFolder, 1)
   if landscapeFiles = {} and portraitFiles = {} then display dialog "No JPG files with orientation metadata were found in the selected folder" buttons {"OK"} cancel button 1 default button 1 with icon stop
   moveFiles(landscapeFiles, landscapeFolder)
   moveFiles(portraitFiles, portraitFolder)
end main

on makeFolders(theFolder, landscapeFolder, portraitFolder)
   tell application "Finder"
       if not (exists folder portraitFolder) then make new folder in theFolder with properties {name:"Portrait"}
       if not (exists folder landscapeFolder) then make new folder in theFolder with properties {name:"Landscape"}
   end tell
end makeFolders

on getFiles(theFolder, theOrientation)
   set theFolder to POSIX path of theFolder
   set theAttributes to "kMDItemFSName == *.jpg && kMDItemOrientation == " & theOrientation
   set theFiles to do shell script "mdfind -onlyin " & quoted form of theFolder & space & quoted form of theAttributes
   
   set text item delimiters to {"/"}
   set theFiles to paragraphs of theFiles
   set sortedFiles to {}
   repeat with aFile in theFiles
       set aFolder to text 1 thru text item -2 of aFile & "/"
       if aFolder = theFolder then set end of sortedFiles to POSIX file aFile
   end repeat
   set text item delimiters to {""}
   return sortedFiles
end getFiles

on moveFiles(theFiles, theFolder)
   tell application "Finder"
       repeat with aFile in theFiles
           try
               move file aFile to theFolder
           on error
               set aFileName to name of file aFile
               display dialog "The file " & quote & aFileName & quote & " already exists in a destination folder" buttons {"Cancel", "Overwrite", "Skip"} cancel button 1 default button 3 with icon caution
               if button returned of result = "Overwrite" then move file aFile to theFolder with replacing
           end try
       end repeat
   end tell
end moveFiles

main()

Last edited by peavine (2022-07-05 10:30:30 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#5 2022-07-05 02:25:04 pm

Stadler
Member
Registered: 2008-07-09
Posts: 14

Re: separate images into folder horizontally and vertically?

Thanks Peavine! 
Working with jpgs.  How can I get to work with png, tif, PSDs, etc?


peavine wrote:

The script included above sorts and moves image files into portrait and landscape folders based on their relative width and height. If the images are digital photos, this may not be a reliable approach, and the following script uses metadata orientation instead. It should be noted that when photos are placed on an external drive, there is a delay in indexing the files, and the following script takes no action if run before indexing is complete. A workaround is to sort the photos on the boot drive. Please test this script with copies of photos until satisfied that the script works as desired.

Applescript:

on main()
   set theFolder to (choose folder)
   set landscapeFolder to (theFolder as text) & "Landscape:"
   set portraitFolder to (theFolder as text) & "Portrait:"
   makeFolders(theFolder, landscapeFolder, portraitFolder)
   set landscapeFiles to getFiles(theFolder, 0)
   set portraitFiles to getFiles(theFolder, 1)
   if landscapeFiles = {} and portraitFiles = {} then display dialog "No JPG files with orientation metadata were found in the selected folder" buttons {"OK"} cancel button 1 default button 1 with icon stop
   moveFiles(landscapeFiles, landscapeFolder)
   moveFiles(portraitFiles, portraitFolder)
end main

on makeFolders(theFolder, landscapeFolder, portraitFolder)
   tell application "Finder"
       if not (exists folder portraitFolder) then make new folder in theFolder with properties {name:"Portrait"}
       if not (exists folder landscapeFolder) then make new folder in theFolder with properties {name:"Landscape"}
   end tell
end makeFolders

on getFiles(theFolder, theOrientation)
   set theFolder to POSIX path of theFolder
   set theAttributes to "kMDItemFSName == *.jpg && kMDItemOrientation == " & theOrientation
   set theFiles to do shell script "mdfind -onlyin " & quoted form of theFolder & space & quoted form of theAttributes
   
   set text item delimiters to {"/"}
   set theFiles to paragraphs of theFiles
   set sortedFiles to {}
   repeat with aFile in theFiles
       set aFolder to text 1 thru text item -2 of aFile & "/"
       if aFolder = theFolder then set end of sortedFiles to POSIX file aFile
   end repeat
   set text item delimiters to {""}
   return sortedFiles
end getFiles

on moveFiles(theFiles, theFolder)
   tell application "Finder"
       repeat with aFile in theFiles
           try
               move file aFile to theFolder
           on error
               set aFileName to name of file aFile
               display dialog "The file " & quote & aFileName & quote & " already exists in a destination folder" buttons {"Cancel", "Overwrite", "Skip"} cancel button 1 default button 3 with icon caution
               if button returned of result = "Overwrite" then move file aFile to theFolder with replacing
           end try
       end repeat
   end tell
end moveFiles

main()

Offline

 

#6 2022-07-05 02:46:56 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1403

Re: separate images into folder horizontally and vertically?

Stadler. The new script is written to work with JPG digital photos, where landscape/portrait orientation is normally determined by how the camera is held. For other images--such as png and tif--the earlier script should be used. I'm not familiar with PSD, which appears to be a Photoshop file format, and don't know which should be used. If the earlier script worked with PSD files then I would stick with that.

Last edited by peavine (2022-07-05 03:14:32 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)