1

我有一台尼康相机,可以输出很好的 NEF 原始文件,而不是很好的 JPEG 文件。我可以使用 Mac OSX 10.6.8 (Snow Leopard) 附带的 Preview 应用程序来简单地打开 NEF 和 SaveAs JPEG 以创建一个大小约为 1/6 的文件,该文件与原始 NEF 几乎无法区分。

[编辑] 这是按需要工作的最终脚本,带有注释和一些错误测试:

(*
 AppleScript to convert Nikon raw NEF files into much smaller JPG files.
 The JPG files will inherit the file date and time of the source NEF files.
 Note that any JPG files in the target folder that have the same name
 as a NEF file in that folder, will be overwritten.
 *)

-- User selects target folder with NEF files to convert and save there. 
set theImageFolder to choose folder with prompt "
Select a folder containing fileⁿ.NEF images to 
convert into JPEG images and SaveAs: fileⁿ.JPG"
set theOutputFolder to theImageFolder

-- Finder locates NEF files, ignoring other file types in the target folder.
tell application "Finder"
    set theImages to every file of theImageFolder whose name extension is "NEF"
end tell

-- Image Events app processes the images.
tell application "Image Events"
    launch
    repeat with a from 1 to length of theImages

        -- Get file name as text string.
        set theImage to file ((item a of theImages) as string)

        -- Get date/time of source NEF file.
        tell application "Finder" to set fileTimestamp to creation date of theImage
        set theImageReference to open theImage
        tell theImageReference
            set theImageName to name

            -- Detect the .NEF extension to replace with .JPG on output.
            set savedDelimiters to AppleScript's text item delimiters

            -- Split filename string into list, using "." as a delimiter.
            set AppleScript's text item delimiters to {"."}
            set delimitedList to every text item of theImageName

            -- Remove the .NEF extension from the list, if it was there.
            ignoring case

                --Process only NEF files.
                if last item of delimitedList is "NEF" then
                    set filenameList to items 1 thru -2 of delimitedList
                    set theImageName to filenameList as string
                end if
            end ignoring

            -- Restore delimiters to default in case it had previously been changed.
            set AppleScript's text item delimiters to savedDelimiters

            -- Construct full path of file to save, with JPG as output file extension.
            set saveImageName to ((theOutputFolder as string) & theImageName & ".JPG")

            -- Check if a file with the output JPG file name is already present in the target folder.
            tell application "Finder"
                if exists file saveImageName then

                    -- Abort script if user doesn't want to overwrite this file and continue.
                    beep
                    if button returned of (display dialog "     An identical JPG file is already at:
" & saveImageName & "

     Would you like to:" buttons {"Replace it and continue", "Abort"} default button "Abort") is "Abort" then exit repeat
                end if
            end tell

            -- SaveAs the file in JPEG format, leaving the source NEF file unmodified.
            set saveImageName to save in saveImageName as JPEG

            --Match the output JPG file date/time to that of the NEF source file.
            tell application "Finder" to set modification date of saveImageName to fileTimestamp
        end tell
    end repeat
end tell
tell application "Finder"
    display alert "Done.  Duplicated selected NEF files in
  " & theOutputFolder & " 
as JPGs with dates/times matching NEFs."
end tell

下面是我最初尝试创建 AppleScript 以节省我在数百个 NEF 文件上使用 Preview 应用程序手动执行此操作所需的时间。它有效,但是这个网站上乐于助人的人帮助我大大改进了它。正如您从初始用户提示中看到的那样,我只想在现有 JPG 文件将被替换的情况下提示用户。我还希望将输出文件名设为n .JPG 而不是n .NEF.jpg 并让输出 JPG 文件继承原始 NEF 文件的创建日期和时间。我欢迎任何建议,但由于我已经走到这一步,我的偏好是避免添加 shell 脚本,如果可能的话,使用 AppleScript 来完成。

set theImageFolder to choose folder with prompt "Note:  This script will replace any existing files in the selected folder matching
the name of a NEF file and end in a JPG extension with a new file of that name.  
For example, X.NEF will create X.JPG and replace any existing file named X.JPG 
that was already in the selected folder (not in any other folders). To begin now,
Select a folder with NEF images to convert into JPEG images:"
set theOutputFolder to theImageFolder

tell application "Finder"
    set theImages to every file of theImageFolder whose name extension is "NEF"
end tell

tell application "Image Events"
    launch
    repeat with a from 1 to length of theImages
        set theImage to file ((item a of theImages) as string)
        set theImageReference to open theImage
        tell theImageReference
            set theImageName to name
            save in ((theOutputFolder as string) & theImageName & ".JPG") as JPEG
        end tell
    end repeat
end tell
tell application "Finder"
    display alert "Done.  All NEF files in the selected folder have been duplicated in JPEG format."
end tell
4

3 回答 3

0

您还可以使用sips来转换图像并touch -r更改修改(和创建)时间:

for f in *.nef; do jpg="${f%nef}jpg"; sips -s format jpeg -s formatOptions 90 "$f" -o "$jpg"; touch -r "$f" "$jpg"; done

touch -r通常只更改修改和访问时间,但如果目标时间在原始创建时间之前,它也会更改创建时间。

如果文件有不同的创建和修改时间,你可以使用SetFileand GetFileInfo

SetFile -m "$(GetFileInfo -m "$f")" "$jpg"; SetFile -d "$(GetFileInfo -d "$f")" "$jpg"

-m更改修改时间并-d更改创建时间。SetFile并且GetFileInfo是命令行工具包的一部分,可以从developer.apple.com/downloads或 Xcode 的首选项中下载。

于 2013-09-09T12:16:59.093 回答
0

Thank you so much, Atomic Toothbrush!

I can't seem to insert a blank line to a Comment or mark as Code here without it saving the Comment, so here's a followup as an Answer. Really it's more of a revised Question. :}

Seems to me it's very close to working as hoped. I replaced the code inside the Repeat section with the fascinating snippet you suggested, though I don't yet fully understand the tricks it's doing. With one file in the target folder the script aborts highlighting the word "alias" with this message:

error "File Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG wasn’t found." number -43 from "Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG"

With two files in the target folder and the "as alias" removed, it creates DSC_2070.JPG just fine but doesn't change the mod date and aborts with this message:

error "Can’t set modification date of \"Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG\" to date \"Wednesday, August 28, 2013 1:03:29 PM\"." number -10006 from modification date of "Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG"

If I run it once to create the JPG file as above, then add the "as alias" back in and run it again it does change the date (for both creation and modification!) to match the source file but then aborts highlighting the last Tell inside the Repeat with this message:

error "File Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG wasn’t found." number -43 from "Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG"

Looks like it's remembering the last file processed because if I rename the file, remove the "as alias" and run it again it aborts highlighting that same last Tell line inside the Repeat with this message referencing the file name that's no longer in the folder:

error "Can’t set modification date of \"Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG\" to date \"Wednesday, August 28, 2013 1:14:19 PM\"." number -10006 from modification date of "Art:Users:me:Desktop:scriptTest:-file:DSC_2070.JPG"

Complete script with Repeat string inserted as tested above:

set theImageFolder to choose folder with prompt "Select a folder with NEF images to convert into JPEG images:"
set theOutputFolder to theImageFolder

tell application "Finder"
    set theImages to every file of theImageFolder whose name extension is "NEF"
end tell

tell application "Image Events"
    launch
    repeat with a from 1 to length of theImages
        set theImage to file ((item a of theImages) as string)
        tell application "Finder" to set fileTimestamp to creation date of theImage
        set theImageReference to open theImage
        tell theImageReference
            set theImageName to name
            set savedDelimiters to AppleScript's text item delimiters
            set AppleScript's text item delimiters to {"."}
            set delimitedList to every text item of theImageName
            ignoring case
                if last item of delimitedList is "NEF" then
                    set filenameList to items 1 thru -2 of delimitedList
                    set theImageName to filenameList as string
                end if
            end ignoring
            set AppleScript's text item delimiters to savedDelimiters
            set saveImageName to ((theOutputFolder as string) & theImageName & ".JPG") as alias
            save in saveImageName as JPEG
            tell application "Finder" to set modification date of saveImageName to fileTimestamp
        end tell
    end repeat
end tell
tell application "Finder"
    display alert "Done.  All NEF files in the selected folder have been duplicated in JPEG format with modification date and time changed to match the NEF source file."
end tell
于 2013-09-09T03:24:38.593 回答
0

从文件名的角度来看,听起来您只需要从文件名中去除 .NEF 扩展名。您可以通过使用“.”将文件名字符串转换为列表来完成此操作。作为分隔符,从列表中删除最后一项,然后将列表重新组合为文件名字符串。我认为应该这样做(插入“重复”块):

    set theImage to file ((item a of theImages) as string)
    tell application "Finder" to set fileTimestamp to creation date of theImage
    set theImageReference to open theImage
    tell theImageReference
        set theImageName to name
        set savedDelimiters to AppleScript's text item delimiters
-- Split filename string into list, using "." as a delimiter
        set AppleScript's text item delimiters to {"."}
        set delimitedList to every text item of theImageName

-- Remove the .NEF extension from the list, if it was there
        ignoring case
            if last item of delimitedList is "NEF" then
                set filenameList to items 1 thru -2 of delimitedList
                set theImageName to filenameList as string
            end if
        end ignoring

-- Restore delimiters to default in case of other users
        set AppleScript's text item delimiters to savedDelimiters

-- Construct full path of file to save
        set saveImageName to ((theOutputFolder as string) & theImageName & ".JPG")

-- Check for file existence
        tell application "Finder"
            if exists file saveImageName then
-- Check with user - skip to the next file if user doesn't want to overwrite
                if button returned of (display dialog saveImageName & " already exists.  Overwrite?" buttons {"Yes", "No"}) is "No" then exit repeat
            end if
        end tell

-- Save the file
        set saveImageName to save in saveImageName as JPEG

-- Fiddle the timestamp of the saved file
        tell application "Finder" to set modification date of saveImageName to fileTimestamp
    end tell

注意我认为你不能轻易更改 .JPG 文件的创建日期(它是 finder 字典中的 ar/o 属性。我能做的最好的是将 .JPG 文件的修改日期设置为创建.NEF 文件的日期。

于 2013-09-07T04:17:28.683 回答