AppleScript Audio

[afconvert]オーディオファイル変換


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# オーディオファイル変換
004(*
005ヘルプ
006/usr/bin/afconvert --help
007フォーマットヘルプ
008/usr/bin/afconvert --help-formats
009変換詳細
010https://developer.apple.com/library/archive/documentation/MusicAudio/Conceptual/CoreAudioOverview/SupportedAudioFormatsMacOSX/SupportedAudioFormatsMacOSX.html
011*)
012#com.cocolog-nifty.quicktimer.icefloe
013----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
014use AppleScript version "2.8"
015use framework "Foundation"
016use scripting additions
017
018property refMe : a reference to current application
019
020set appFileManager to refMe's NSFileManager's defaultManager()
021
022#############################
023###ダイアログ ファイル選択
024set strName to (name of current application) as text
025if strName is "osascript" then
026  tell application "Finder" to activate
027else
028  tell current application to activate
029end if
030#デスクトップ
031set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
032set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
033set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
034#ダイアログ設定
035set listUTI to {"public.audio"}
036set strMes to ("サウンドファイルを選んでください") as text
037set strPrompt to ("サウンドファイルを選んでください") as text
038try
039  ### ファイル選択時
040  set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, multiple selections allowed and showing package contents) as list
041on error
042  log "エラーしました"
043  return "エラーしました"
044end try
045if listAliasFilePath is {} then
046  log "選んでください"
047  doResExec()
048end if
049
050#############################
051###フォーマット(変換後)選択
052set recordExtension to {|3GP Audio|:"3gp", |3GPP-2|:"3gp2", |AAC ADTS|:"adts", |AIFC|:"aifc", |AIFF|:"aiff", |Apple MPEG-4 Audio|:"m4a", |Apple MPEG-4 AudioBooks|:"m4b", |Core Audio Format|:"caf", |FLAC|:"flac", |LOAS|:"loas", |LATM|:"latm", |MPEG-4 Audio|:"mp4", |WAVE|:"wav", |WAVE-BW64|:"wav", |WAVE-RF64|:"wav", |WAVE-W64F|:"w64"} as record
053
054##MP3等変換できないものがある
055#set recordExtension to {|3GP Audio|:"3gp", |3GPP-2|:"3gp2", |AAC ADTS|:"adts", |AC3|:"ac3", |AIFC|:"aifc", |AIFF|:"aiff", |AMR|:"amr", |Apple MPEG-4 Audio|:"m4a", |Apple MPEG-4 AudioBooks|:"m4b", |Core Audio Format|:"caf", |EC3|:"eac3", |FLAC|:"flac", |LOAS|:"loas", |LATM|:"latm", |MPEG Layer 1|:"mp1", |MPEG Layer 2|:"m2a", |MPEG Layer 3|:"mp3", |MPEG-4 Audio|:"mp4", |NeXT/Sun|:"au", |Sound Designer II|:"sd2", |WAVE|:"wav", |WAVE-BW64|:"wav", |WAVE-RF64|:"wav", |WAVE-W64F|:"w64"} as record
056#キーでリスト
057set ocidExtensionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
058ocidExtensionDict's setDictionary:(recordExtension)
059set ocidDataKeys to ocidExtensionDict's allKeys()
060#ソートして
061set listDataKeys to (ocidDataKeys's sortedArrayUsingSelector:("localizedStandardCompare:")) as list
062#
063set strTitle to ("選んでください") as text
064set strPrompt to ("変換後のフォーマットを選んでください") as text
065try
066  set listResponse to (choose from list listDataKeys with title strTitle with prompt strPrompt default items (item 8 of listDataKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
067on error
068  log "エラーしました"
069  return "エラーしました"
070end try
071if (item 1 of listResponse) is false then
072  return "キャンセルしましたA"
073else if (item 1 of listResponse) is "キャンセル" then
074  return "キャンセルしましたB"
075else
076  set strDataType to (item 1 of listResponse) as text
077end if
078set strExtension to (ocidExtensionDict's objectForKey:(strDataType)) as text
079
080###
081set recordExtension to {|3GP Audio|:{"aac", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "samr"}, |3GPP-2|:{"aac", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "samr"}, |AAC ADTS|:{"aac ", "aach", "aacp"}, |AC3|:{"ac-3"}, |AIFC|:{"I8", "BEI16", "BEI24", "BEI32", "BEF32", "BEF64", "UI8", "ulaw", "alaw", "MAC3", "MAC6", "ima4", "QDMC", "QDM2", "Qclp", "agsm"}, |AIFF|:{"I8", "BEI16", "BEI24", "BEI32"}, |AMR|:{"samr", "sawb"}, |Apple MPEG-4 Audio|:{".mp1", ".mp2", ".mp3", "aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "ac-3", "alac", "ec-3", "flac", "paac", "pac3", "pec3"}, |Apple MPEG-4 AudioBooks|:{"aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "paac"}, |Core Audio Format|:{".mp1", ".mp2", ".mp3", "aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "ac-3", "alac", "alaw", "apac", "dvi8", "ec-3", "flac", "ilbc", "ima4", "opus", "paac", "pac3", "pach", "pacp", "pec3", "pusa", "qaac", "qac3", "qach", "qacp", "qapc", "qec3", "qflc", "qlac", "qusa", "samr", "ulaw", "usac", "zaac", "zac3", "zach", "zacp", "zec3", "zlac", "I8", "BEI16", "BEI24", "BEI32", "BEF32", "BEF64", "UI8"}, |EC3|:{"ec-3"}, |FLAC|:{"flac"}, |LOAS|:{"aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "usac"}, |LATM|:{"aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "usac"}, |MPEG Layer 1|:{".mp1"}, |MPEG Layer 2|:{".mp2"}, |MPEG Layer 3|:{".mp3"}, |MPEG-4 Audio|:{".mp1", ".mp2", ".mp3", "aac ", "aace", "aacf", "aacg", "aach", "aacl", "aacp", "ac-3", "alac", "apac", "ec-3", "flac", "opus", "paac", "pac3", "pach", "pacp", "pec3", "pusa", "qaac", "qac3", "qach", "qacp", "qapc", "qec3", "qflc", "qlac", "qusa", "usac", "LEF32", "BEF32", "LEF64", "BEF64", "LEI32", "BEI32", "UI0", "UI0", "UI0", "UI0"}, |NeXT/Sun|:{"I8", "BEI16", "BEI24", "BEI32", "BEF32", "BEF64", "ulaw", "alaw"}, |Sound Designer II|:{"I8", "BEI16", "BEI24", "BEI32"}, |WAVE|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}, |WAVE-BW64|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}, |WAVE-RF64|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}, |WAVE-W64F|:{"UI8", "LEI16", "LEI24", "LEI32", "LEF32", "LEF64", "ulaw", "alaw"}} as record
082set ocidFormatDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
083ocidFormatDict's setDictionary:(recordExtension)
084set listDataFormat to (ocidFormatDict's objectForKey:(strDataType)) as list
085#
086set strTitle to ("選んでください") as text
087set strPrompt to ("変換後のデータ・フォーマットを選んでください") as text
088try
089  set listResponse to (choose from list listDataFormat with title strTitle with prompt strPrompt default items (item 1 of listDataFormat) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
090on error
091  log "エラーしました"
092  return "エラーしました"
093end try
094if (item 1 of listResponse) is false then
095  return "キャンセルしましたA"
096else if (item 1 of listResponse) is "キャンセル" then
097  return "キャンセルしましたB"
098else
099  set strDataFormat to (item 1 of listResponse) as text
100end if
101###
102set recordOutPutDataFormat to {|3GP Audio|:"3gpp", |3GPP-2|:"3gp2", |AAC ADTS|:"adts", |AC3|:"ac-3", |AIFC|:"AIFC", |AIFF|:"AIFF", |AMR|:"amrf", |Apple MPEG-4 Audio|:"m4af", |Apple MPEG-4 AudioBooks|:"m4bf", |Core Audio Format|:"caff", |EC3|:"ec-3", |FLAC|:"flac", |LOAS|:"loas", |LATM|:"loas", |MPEG Layer 1|:"MPG1", |MPEG Layer 2|:"MPG2", |MPEG Layer 3|:"MPG3", |MPEG-4 Audio|:"mp4f", |NeXT/Sun|:"NeXT", |Sound Designer II|:"Sd2f", |WAVE|:"WAVE", |WAVE-BW64|:"BW64", |WAVE-RF64|:"RF64", |WAVE-W64F|:"W64f"} as record
103set ocidOutPutDataFormat to refMe's NSDictionary's alloc()'s initWithDictionary:(recordOutPutDataFormat)
104set strOutPutDataFormat to (ocidOutPutDataFormat's valueForKey:(strDataType))
105
106repeat with itemFilePath in listAliasFilePath
107  #入力ファイル
108  set aliasFilePath to itemFilePath as alias
109  set strFilePath to (POSIX path of aliasFilePath) as text
110  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
111  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
112  set ocidFileName to ocidFilePath's lastPathComponent()
113  #BaseFilePath
114  set ocidBaseFilePath to ocidFilePath's stringByDeletingPathExtension()
115  set ocidSaveFilePath to (ocidBaseFilePath's stringByAppendingPathExtension:(strExtension)) as text
116  #
117  set boolDirExists to (appFileManager's fileExistsAtPath:(ocidSaveFilePath) isDirectory:(true))
118  if boolDirExists = true then
119    return "上書きになるので中止します"
120  else if boolDirExists = false then
121    log (ocidFileName as text) & ":処理開始"
122  end if
123  
124  ########
125  if strDataFormat is "m4af" then
126    #ラウドネス情報付き
127    set strCommandText to ("/usr/bin/afconvert -v \"" & ocidFilePath & "\" -o \"" & ocidSaveFilePath & "\" -f '" & strOutPutDataFormat & "' -d '" & strDataFormat & "' --quality 127 --src-quality 127 --soundcheck-generate --media-kind \"Music\"") as text
128  else
129    set strCommandText to ("/usr/bin/afconvert -v \"" & ocidFilePath & "\" -o \"" & ocidSaveFilePath & "\" -f '" & strOutPutDataFormat & "' -d '" & strDataFormat & "' --quality 127 --src-quality 127") as text
130  end if
131  log strCommandText
132  try
133    do shell script strCommandText
134  on error
135    return strFilePath & "\nでエラーしました"
136  end try
137end repeat
138
139##############################
140#####自分自身を再実行
141##############################
142to doResExec()
143  if button returned of recordResult is "再実行" then
144    tell application "Finder"
145      set aliasPathToMe to (path to me) as alias
146    end tell
147    run script aliasPathToMe with parameters "再実行"
148  end if
149end doResExec
AppleScriptで生成しました

|

オーディオの入出力切り替え


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004参考
005https://www.macscripter.net/t/switchaudio-a-command-line-tool-to-change-the-audio-input-and-output-device/75630
006バイナリーへの直リンク
007https://klieme.ch/pub/SwitchAudio.dmg
008*)
009# com.cocolog-nifty.quicktimer.icefloe
010----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use scripting additions
015
016property refMe : a reference to current application
017#バイナリーへのパス
018set strBinPath to ("~/bin/SwitchAudio/SwitchAudio") as text
019set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
020set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
021#リスト取得
022set strCommandText to ("\"" & ocidBinPath & "\"  -l") as text
023set strResponse to (do shell script strCommandText) as text
024#入出力でわける
025set strDelim to AppleScript's text item delimiters
026set AppleScript's text item delimiters to "\r\r"
027set listResponse to every text item of strResponse as list
028set AppleScript's text item delimiters to strDelim
029#
030set strResponeInput to (item 1 of listResponse) as text
031set strResponeOutput to (item 2 of listResponse) as text
032#個別項目へ
033set strDelim to AppleScript's text item delimiters
034set AppleScript's text item delimiters to "\r"
035set listInput to every text item of strResponeInput as list
036set listOutput to every text item of strResponeOutput as list
037set listChooser to every text item of strResponse as list
038set AppleScript's text item delimiters to strDelim
039#ダイアログ
040set strName to (name of current application) as text
041if strName is "osascript" then
042  tell application "Finder" to activate
043else
044  tell current application to activate
045end if
046set strTitle to ("選んでください") as text
047set strPrompt to ("切り替えるデバイスを選んでください") as text
048try
049  set listChoose to (choose from list listChooser with title strTitle with prompt strPrompt default items (item 2 of listChooser) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
050on error
051  log "エラーしました"
052  return "エラーしました"
053end try
054if (item 1 of listChoose) is false then
055  return "キャンセルしましたA"
056else if (item 1 of listChoose) is "キャンセル" then
057  return "キャンセルしましたB"
058else
059  set strChoose to (item 1 of listChoose) as text
060end if
061if strChoose is "Input Devices:" then
062  log "デバイス以外を選択"
063  doReload()
064else if strChoose is "Output Devices:" then
065  log "デバイス以外を選択"
066  doReload()
067else if strChoose is "" then
068  log "デバイス以外を選択"
069  doReload()
070else if listInput contains strChoose then
071  log "入力デバイスが選択されていました"
072  set strCommandText to ("\"" & ocidBinPath & "\"  -i \"" & strChoose & "\"") as text
073  try
074    do shell script strCommandText
075  end try
076else if listOutput contains strChoose then
077  log "出力デバイスが選択されていました"
078  set strCommandText to ("\"" & ocidBinPath & "\"  -o \"" & strChoose & "\"") as text
079  try
080    do shell script strCommandText
081  end try
082end if
083
084
085###デバイス以外を選択したら
086to doReload()
087  #自分自信を再実行
088  tell application "Finder"
089    set aliasPathToMe to (path to me) as alias
090  end tell
091  run script aliasPathToMe with parameters "再実行"
092end doReload
AppleScriptで生成しました

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat BookMark Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Open Acrobat Plugin Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat Sequ Acrobat Sign Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe RemoteUpdateManager Apple AppleScript AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Audio AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript Guide AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript Mail AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFileManager enumeratorAtURL AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Events Plist AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML savedSearch AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript XMP AppleScript YouTube AppleScript Zero Suppress Applications CityCode Design github iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom