基于方向的过滤图像AppleScript无法正确响应

发布于 2025-01-21 22:23:07 字数 5925 浏览 3 评论 0原文

我正在研究一个项目,每天都必须在2个文件夹,景观和肖像中少量照片。 找到了这个applescript

global logFolder, logFilename 
global landscapeFolderPath, portraitFolderPath 
global landscapeFolder, portraitFolder 
 
-- folder action entry 
on adding folder items to sourceFolder after receiving itemList 
 
    -- set log folder 
    set sourceFilename to my FilenameFromPath(sourceFolder) 
    set sourceBasename to my ExtensionFromFilename(sourceFilename) 
    set logFilename to (sourceBasename & ".log") as text 
    set logFolder to sourceFolder 
 
    -- calculate default destination folder paths 
    set sourceFolderPath to (the POSIX path of (sourceFolder)) as text 
    set landscapeFolderPath to (sourceFolderPath & "Landscape/") as text 
    set landscapeFolder to (POSIX file landscapeFolderPath) 
    set portraitFolderPath to (sourceFolderPath & "Portrait/") as text 
    set portraitFolder to (POSIX file portraitFolderPath) 
 
    -- create destination folders if needed 
    my MakeDirectory(landscapeFolderPath) 
    my MakeDirectory(portraitFolderPath) 
 
    -- process received folders 
    repeat with nextItem in itemList 
        my ProcessFile(nextItem) 
    end repeat 
end adding folder items to 
 
on ProcessFile(nextItem) 
    -- coerce to alias 
    set nextFile to nextItem as alias 
 
    -- get file name and type 
    tell application "Finder" 
        set nextFilename to (the name of nextFile) as text 
        set nextFiletype to (the kind of nextFile) as text 
    end tell 
 
    -- get POSIX path 
    set nextFilePath to (nextFile's POSIX path) as text 
 
    -- process only image files 
    if nextFiletype ends with "image" then 
 
        -- read image data 
        tell application "Image Events" 
            set imageData to open nextFile 
            set imageDimensions to dimensions of imageData 
            close imageData 
        end tell 
 
        -- get image dimensions 
        set imageWidth to item 1 of imageDimensions as integer 
        set imageHeight to item 2 of imageDimensions as integer 
 
        -- determine image orientation 
        if imageWidth is greater than imageHeight then 
 
            -- this image uses landscape orientation 
            my LogEntry("Moving " & the quoted form of nextFilename & " to: " & the quoted form of my FilenameFromPath(the POSIX path of landscapeFolder, false) as text) 
            tell application "Finder" to move nextFile to landscapeFolder 
        else 
 
            -- this image uses portrait orientation 
            my LogEntry("Moving " & the quoted form of nextFilename & " to: " & the quoted form of my FilenameFromPath(the POSIX path of portraitFolder, false) as text) 
            tell application "Finder" to move nextFile to portraitFolder 
        end if 
    end if 
end ProcessFile 
 
on MakeDirectory(directoryPath) 
    do shell script ("mkdir -p " & the quoted form of directoryPath as text) as text 
end MakeDirectory 
 
on ExtensionFromFilename(filename) 
    set saveDelims to AppleScript's text item delimiters 
    set AppleScript's text item delimiters to {"."} 
    set itemList to (every text item of filename) 
    set AppleScript's text item delimiters to saveDelims 
    set justTheExtension to itemList's last item 
end ExtensionFromFilename 
 
on FilenameFromPath(somePath) 
    set somePath to somePath as text 
    -- determine path type & delimiter 
    if somePath contains ":" then 
        set delim to ":" 
    else if somePath contains "/" then 
        set delim to "/" 
    else 
        return ("ERROR in FilenameFromPath - could not determine delimiter from " & somePath) as text 
    end if 
    -- split it 
    set oldDelimiters to AppleScript's text item delimiters 
    set AppleScript's text item delimiters to delim 
    set pathAsList to text items of (somePath as text) 
    if the last character of (somePath as text) is delim then 
        set idx to -2 
    else 
        set idx to -1 
    end if 
    set filename to item idx of pathAsList 
    set AppleScript's text item delimiters to oldDelimiters 
    return filename 
end FilenameFromPath 
 
on FormatDateTime(theDate) 
    set theDate to theDate as date 
    set dd to text -2 thru -1 of ("0" & theDate's day) 
    copy theDate to tempDate 
    set the month of tempDate to January 
    set mm to text -2 thru -1 of ¬ 
        ("0" & 1 + (theDate - tempDate + 1314864) div 2629728) 
    set yy to text -1 thru -4 of ((year of theDate) as text) 
    set tt to time string of theDate 
    return (yy & "-" & mm & "-" & dd & " " & tt as text) 
end FormatDateTime 
 
on LogEntry(someText) 
    global logFolder, logFilename 
    try 
        set logFile to (logFolder as text) & logFilename 
        set logRef to open for access (file logFile) with write permission 
        if logRef ≠ 0 then 
            write FormatDateTime(current date) & ": " & someText & return starting at eof to logRef 
            close access logRef 
        end if 
    on error errorMessage number errorNumber 
        tell application "System Events" to display alert "Could not add log entry" message "ERROR: " & errorMessage & " (" & errorNumber & ")" 
    end try 
end LogEntry 

我从这里

https://www.quora.com/can-i-create-create-an-automator-folder-action-action-action-action-that-move-that-move-images-- to-two-different-folders-thutheyre-theyre-in-in-in-landscape-or-portrait-size

面临这个问题

我 “ nofollow noreferrer”> https://youtube.com/shorts/prticogm7w0

似乎脚本在复制结束之前开始。我不熟悉applescript,因此任何帮助都会受到欢迎。

I am working on a project that i have to short ton of photos every day in 2 folders, landscape and portrait. I found this applescript

global logFolder, logFilename 
global landscapeFolderPath, portraitFolderPath 
global landscapeFolder, portraitFolder 
 
-- folder action entry 
on adding folder items to sourceFolder after receiving itemList 
 
    -- set log folder 
    set sourceFilename to my FilenameFromPath(sourceFolder) 
    set sourceBasename to my ExtensionFromFilename(sourceFilename) 
    set logFilename to (sourceBasename & ".log") as text 
    set logFolder to sourceFolder 
 
    -- calculate default destination folder paths 
    set sourceFolderPath to (the POSIX path of (sourceFolder)) as text 
    set landscapeFolderPath to (sourceFolderPath & "Landscape/") as text 
    set landscapeFolder to (POSIX file landscapeFolderPath) 
    set portraitFolderPath to (sourceFolderPath & "Portrait/") as text 
    set portraitFolder to (POSIX file portraitFolderPath) 
 
    -- create destination folders if needed 
    my MakeDirectory(landscapeFolderPath) 
    my MakeDirectory(portraitFolderPath) 
 
    -- process received folders 
    repeat with nextItem in itemList 
        my ProcessFile(nextItem) 
    end repeat 
end adding folder items to 
 
on ProcessFile(nextItem) 
    -- coerce to alias 
    set nextFile to nextItem as alias 
 
    -- get file name and type 
    tell application "Finder" 
        set nextFilename to (the name of nextFile) as text 
        set nextFiletype to (the kind of nextFile) as text 
    end tell 
 
    -- get POSIX path 
    set nextFilePath to (nextFile's POSIX path) as text 
 
    -- process only image files 
    if nextFiletype ends with "image" then 
 
        -- read image data 
        tell application "Image Events" 
            set imageData to open nextFile 
            set imageDimensions to dimensions of imageData 
            close imageData 
        end tell 
 
        -- get image dimensions 
        set imageWidth to item 1 of imageDimensions as integer 
        set imageHeight to item 2 of imageDimensions as integer 
 
        -- determine image orientation 
        if imageWidth is greater than imageHeight then 
 
            -- this image uses landscape orientation 
            my LogEntry("Moving " & the quoted form of nextFilename & " to: " & the quoted form of my FilenameFromPath(the POSIX path of landscapeFolder, false) as text) 
            tell application "Finder" to move nextFile to landscapeFolder 
        else 
 
            -- this image uses portrait orientation 
            my LogEntry("Moving " & the quoted form of nextFilename & " to: " & the quoted form of my FilenameFromPath(the POSIX path of portraitFolder, false) as text) 
            tell application "Finder" to move nextFile to portraitFolder 
        end if 
    end if 
end ProcessFile 
 
on MakeDirectory(directoryPath) 
    do shell script ("mkdir -p " & the quoted form of directoryPath as text) as text 
end MakeDirectory 
 
on ExtensionFromFilename(filename) 
    set saveDelims to AppleScript's text item delimiters 
    set AppleScript's text item delimiters to {"."} 
    set itemList to (every text item of filename) 
    set AppleScript's text item delimiters to saveDelims 
    set justTheExtension to itemList's last item 
end ExtensionFromFilename 
 
on FilenameFromPath(somePath) 
    set somePath to somePath as text 
    -- determine path type & delimiter 
    if somePath contains ":" then 
        set delim to ":" 
    else if somePath contains "/" then 
        set delim to "/" 
    else 
        return ("ERROR in FilenameFromPath - could not determine delimiter from " & somePath) as text 
    end if 
    -- split it 
    set oldDelimiters to AppleScript's text item delimiters 
    set AppleScript's text item delimiters to delim 
    set pathAsList to text items of (somePath as text) 
    if the last character of (somePath as text) is delim then 
        set idx to -2 
    else 
        set idx to -1 
    end if 
    set filename to item idx of pathAsList 
    set AppleScript's text item delimiters to oldDelimiters 
    return filename 
end FilenameFromPath 
 
on FormatDateTime(theDate) 
    set theDate to theDate as date 
    set dd to text -2 thru -1 of ("0" & theDate's day) 
    copy theDate to tempDate 
    set the month of tempDate to January 
    set mm to text -2 thru -1 of ¬ 
        ("0" & 1 + (theDate - tempDate + 1314864) div 2629728) 
    set yy to text -1 thru -4 of ((year of theDate) as text) 
    set tt to time string of theDate 
    return (yy & "-" & mm & "-" & dd & " " & tt as text) 
end FormatDateTime 
 
on LogEntry(someText) 
    global logFolder, logFilename 
    try 
        set logFile to (logFolder as text) & logFilename 
        set logRef to open for access (file logFile) with write permission 
        if logRef ≠ 0 then 
            write FormatDateTime(current date) & ": " & someText & return starting at eof to logRef 
            close access logRef 
        end if 
    on error errorMessage number errorNumber 
        tell application "System Events" to display alert "Could not add log entry" message "ERROR: " & errorMessage & " (" & errorNumber & ")" 
    end try 
end LogEntry 

from here

https://www.quora.com/Can-I-create-an-automator-folder-action-that-move-images-to-two-different-folders-whether-theyre-in-landscape-or-portrait-size

and i am facing this problem

https://youtube.com/shorts/PrtIcOGM7w0

it seems like scripts start before copy ends. I am not familiar with applescript, so any help would be welcomed ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2025-01-28 22:23:07

尝试在手表文件夹外制作目标文件夹。

Try making destination folders outside the watched folder.

紫瑟鸿黎 2025-01-28 22:23:07

感谢您的建议,但是如果不涉及复制,就解决了问题,只需转到观看的文件夹,景观和肖像文件夹已经创建的作品就像魅力一样

thanks for your suggestion, but problem solved if not copy involved, just move to watched folder and Landscape and Portrait folders already created works like a charm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文