簡體   English   中英

AppleScript Droplet 將 PSD 和 TIF 轉換為 JPG

[英]AppleScript Droplet to Convert PSD and TIF to JPG

有很多轉換為 JPG 的示例,我正在嘗試根據我的需要更改一個,但我需要的幫助很少。 要求是:

  1. 它應該是 AppleScript 液滴。 我正在使用腳本編輯器(出於某種原因,Automator 無法為我運行簡單的液滴拖放 function)。
  2. JPG 的 output 文件夾不應由用戶提示.. 而是在代碼中永久設置為變量並且易於更改。
  3. 轉換后的 JPG 的質量(壓縮)也應該可以在代碼中輕松自定義。
  4. 如有必要,轉換后的 JPG 文件必須轉換為顏色配置文件 Adobe RGB 1998。

我知道圖像事件允許我們設置 JPG 壓縮,例如:

save openedFile as JPEG with compression level (low|medium|high)

但不幸的是我需要更多的定制。

shell 腳本將幫助我將級別設置為 10 到 100,但不幸的是我無法正確實現 shell 腳本。 請對第 3 點和第 4 點提供一點幫助。謝謝!

on run
    display dialog "Please drag image files to this script to turn them into JPEGs"
end run
on open draggeditems
    set End_Folder to "Macintosh HD:Users:zzz:Desktop:End"
    
    repeat with currentFile in draggeditems
        tell application "Image Events"
            set openedFile to open (currentFile as alias)
            set fileLocation to the location of openedFile
            set fileName to the name of openedFile
            set Path_to_Converted_File to (End_Folder & ":" & text 1 thru -5 of fileName & ".jpg")

            do shell script "sips --setProperty formatOptions 10 " & openedFile
            
    save openedFile as JPEG in Path_to_Converted_File
            --save openedFile as JPEG  with compression level low  in Path_to_Converted_File (low|medium|high)
            
            close openedFile
        end tell
    end repeat
end open

圖像事件sips混合起來比有用更令人困惑,而且由於sips可以執行您正在尋找的各種選項(例如 3 和 4),因此將它用於所有事情更有意義。 為各種選項設置一些變量將使您可以根據需要更改它們,或者添加首選項等。 sips手冊頁將為您提供有關各種選項的更多詳細信息; 我為以下腳本中使用的注釋添加了注釋:

on run
    open (choose file with prompt "Select image files to turn into JPEGs:" with multiple selections allowed)
end run

on open draggeditems
    set destination to (((path to desktop folder) as text) & "End:") -- folder path (trailing delimiter)
    set format to "jpeg" -- jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga
    set extension to ".jpg" -- extension to match format
    set compression to 10 -- low | normal | high | best | <percent>
    set profile to quoted form of "/System/Library/ColorSync/Profiles/AdobeRGB1998.icc" -- POSIX path to color profile
    repeat with thisFile in draggeditems
        set theName to justTheName for thisFile
        set originalFile to quoted form of POSIX path of thisFile
        set convertedFile to quoted form of POSIX path of (destination & theName & extension)
        do shell script "sips -s format " & format & " -s formatOptions " & compression & " -m " & profile & space & originalFile & " --out " & convertedFile
    end repeat
end open

on justTheName for filePath
    tell application "System Events" to tell disk item (filePath as text)
        set {fileName, extension} to {name, name extension}
    end tell
    if extension is not "" then set fileName to text 1 thru -((count extension) + 2) of fileName -- just the name part
    return fileName
end justTheName

編輯添加: shell 腳本需要 POSIX 路徑,因此傳遞給open處理程序的別名在它們包含空格的情況下被強制和引用。

你的劇本有很多錯誤。 目前尚不清楚為什么要在將文件發送到 shell 命令之前打開文件進行寫入。 sips 不對打開的 FILE 引用進行操作。 sips 需要從 POSIX 路徑打開文件。 我認為這可能會做你想要的(你需要實現錯誤檢查等):

on open draggeditems
    set End_Folder to "~/Desktop/End/"
    repeat with currentFile in draggeditems
        do shell script "sips --setProperty formatOptions 10 " & quoted form of POSIX path of currentFile & " --out " & End_Folder
    end repeat
end open

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM