1

私は素晴らしい NEF RAW ファイルを出力するニコンのカメラを持っていますが、それほど素晴らしい JPEG ファイルは出力しません。Mac OSX 10.6.8 (Snow Leopard) に付属の Preview アプリを使用して NEF を開き、SaveAs JPEG を使用して、元の NEF とほぼ見分けがつかないサイズの約 1/6 のファイルを作成できます。

[編集] 以下は、コメントといくつかのエラー テストを含む、希望どおりに機能する最終的なスクリプトです。

(*
 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

以下は、何百もの NEF ファイルに対して Preview アプリを使って手動でこれを行うのにかかる時間を節約するために、AppleScript を作成する最初の試みでした。それは機能しますが、このウェブサイトの親切な人々は私がそれを大幅に改善するのを助けてくれました. 最初のユーザー プロンプトからわかるように、既存の JPG ファイルが置き換えられる場合にのみ、ユーザーにプロンプ​​トを表示するようにしました。また、出力ファイル名をn .NEF.jpgではなくn .JPGにして、出力 JPG ファイルに元の NEF ファイルの作成日時を継承させたいと考えていました。私はどんな提案も歓迎したが、私はすでにここまでやってきたので、シェルスクリプトを追加するのを控え、可能であればすべて 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

ファイル名の観点からは、ファイル名から .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 ファイルの作成日を簡単に変更できるとは思わないことに注意してください (これはファインダー辞書のプロパティです。私ができる最善の方法は、.JPG ファイルの変更日を作成日に設定することです。 .NEF ファイルの日付。

于 2013-09-07T04:17:28.683 に答える
0

アトミック歯ブラシ、ありがとうございました!

コメントに空白行を挿入したり、コメントを保存せずにここでコードとしてマークしたりすることはできないようです。そのため、回答としてフォローアップします。本当にそれは改訂された質問です。:}

私には、期待どおりに機能することに非常に近いようです。Repeat セクション内のコードを、提案された魅力的なスニペットに置き換えましたが、それが行っているトリックはまだ完全には理解していません。ターゲット フォルダに 1 つのファイルがあると、スクリプトは中止され、「エイリアス」という単語が強調表示され、次のメッセージが表示されます。

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"

ターゲット フォルダー内の 2 つのファイルと「エイリアスとして」を削除すると、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ファイルを作成し、「別名として」を追加して再度実行すると、ソースファイルと一致するように(作成と変更の両方で)日付が変更されますが、最後の強調表示が中止されますRepeat 内で次のメッセージを伝えます。

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"

ファイルの名前を変更し、「エイリアスとして」を削除して再度実行すると、Repeat内の同じ最後の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"

上記でテストしたように Repeat 文字列が挿入された完全なスクリプト:

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 に答える