AppleScript将NEF转换为JPG,保留创建日期/时间

时间:2013-09-07 01:26:27

标签: image macos applescript jpeg converter

我有一台尼康相机可以输出很棒的NEF原始文件,而不是那么棒的JPEG文件。我可以使用我的Mac OSX 10.6.8(Snow Leopard)附带的预览应用程序来简单地打开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文件上使用预览应用程序手动执行此操作所需的时间。它有效,但这个网站上有帮助的人帮助我大大改善了它。从初始用户提示中可以看出,我只想在将替换现有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

3 个答案:

答案 0 :(得分: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文件上的创建日期(它是查找器字典中的ar / o属性。我能做的最好的事情是设置.JPG文件的修改日期到.NEF文件的创建日期。

答案 1 :(得分:0)

非常感谢所以,Atomic Toothbrush!

我似乎无法在评论中插入空白行或在此处标记为代码而不保存评论,因此这里是一个后续答案。真的,它更像是一个修订过的问题。 :}

在我看来,非常接近按预期工作。我用你建议的迷人片段替换了重复部分内的代码,尽管我还没有完全理解它正在做的技巧。如果目标文件夹中有一个文件,则脚本将使用以下消息中止突出显示“别名”一词:

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"

如果目标文件夹中有两个文件并且删除了“as alias”,则会创建DSC_2070.JPG,但不会更改mod日期并使用此消息中止:

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"

如果我运行一次以创建JPG文件,然后再添加“as alias”并再次运行它会更改日期(用于创建和修改!)以匹配源文件但是然后中止使用此消息突出显示重复内部的最后一个Tell:

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"

看起来它正在记住处理的最后一个文件,因为如果我重命名该文件,删除“as alias”并再次运行它会中止突出显示重复内部的同一条最后一条Tell行,此消息引用不再存在的文件名文件夹:

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"

完成脚本,插入重复字符串,如上所示:

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

答案 2 :(得分: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通常只更改修改和访问时间,但如果目标时间早于原始创建时间,它也会更改创建时间。

如果文件的创建和修改时间不同,您可以使用SetFileGetFileInfo

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

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