| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 | ちょっとエスケープに工夫した |
| 007 |
|
| 008 | com.cocolog-nifty.quicktimer.icefloe *) |
| 009 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 010 | use AppleScript version "2.8" |
| 011 | use framework "Foundation" |
| 012 | use scripting additions |
| 013 | property refMe : a reference to current application |
| 014 |
|
| 015 |
|
| 016 | ############################# |
| 017 | ###入力ファイル |
| 018 | ############################# |
| 019 | tell current application |
| 020 | set strName to name as text |
| 021 | end tell |
| 022 | if strName is "osascript" then |
| 023 | tell application "Finder" to activate |
| 024 | else |
| 025 | tell current application to activate |
| 026 | end if |
| 027 | #デスクトップ |
| 028 | set aliasDesktopDirPath to (path to desktop from user domain) as alias |
| 029 | set listUTI to {"public.movie"} as list |
| 030 | set strTitle to ("ファイル選択") as text |
| 031 | set strPrompt to ("ムービーファイルを選んでください") as text |
| 032 | tell application "SystemUIServer" |
| 033 | activate |
| 034 | set listAliasFilePath to (choose file with prompt (strPrompt) default location (aliasDesktopDirPath) of type (listUTI) with invisibles, showing package contents and multiple selections allowed) as list |
| 035 | end tell |
| 036 |
|
| 037 | #NSURLのArrayを作成して |
| 038 | set ocidFileURLArrayM to refMe's NSMutableArray's alloc()'s init() |
| 039 |
|
| 040 | repeat with itemAliasFilePath in listAliasFilePath |
| 041 | set aliasFilePath to itemAliasFilePath as alias |
| 042 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 043 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 044 | set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping() |
| 045 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 046 | set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath() |
| 047 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 048 | (ocidFileURLArrayM's addObject:(ocidFilePathURL)) |
| 049 | end repeat |
| 050 |
|
| 051 | #ソート |
| 052 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:") |
| 053 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
| 054 | set ocidFileURLArrayS to ocidFileURLArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
| 055 |
|
| 056 | #Arrayのアイテム数 |
| 057 | set numCntArray to ocidFileURLArrayS's |count|() |
| 058 |
|
| 059 | repeat with itemIntNo from 0 to (numCntArray - 1) by 1 |
| 060 | #開くムービーのエイリアス・パスをエイリアスリストから取得して |
| 061 | set ocidItemFileURL to (ocidFileURLArrayS's objectAtIndex:(itemIntNo)) |
| 062 | set ocidItemFile to ocidItemFileURL's |path|() |
| 063 | set ocidItemFileENC to doPathEscape(ocidItemFile) |
| 064 | #SWIFT |
| 065 | set strSwiftCode to ("#!/usr/bin/env swift\n//coding: utf-8\nimport Foundation\nimport AVFoundation\nlet strFilePath = \\\"" & ocidItemFileENC & "\\\"\nlet urlFilePath: URL = URL(fileURLWithPath: strFilePath)\nlet refAsset: AVURLAsset = AVURLAsset(url: urlFilePath)\nTask {\nlet arrayTracks: [AVAssetTrack] = try await refAsset.loadTracks(withMediaType: AVMediaType.video)\nif arrayTracks.isEmpty {\nprint(\\\"false\\\")\n} else {\nlet videoTrack: AVAssetTrack = arrayTracks[0]\nlet sizeVideo: CGSize = try await videoTrack.load(.naturalSize)\nlet actualWidth: CGFloat = abs(sizeVideo.width)\nlet actualHeight: CGFloat = abs(sizeVideo.height)\nprint(\\\"\\(Int(actualWidth))\\t\\(Int(actualHeight))\\\")\n}\n}\nThread.sleep(forTimeInterval: 1.0)") as text |
| 066 | #実行する |
| 067 | set strCmd to ("/bin/echo \"" & strSwiftCode & "\" | /usr/bin/swift - ") |
| 068 | set strStdOut to (do shell script strCmd) as text |
| 069 | |
| 070 | set strDelim to AppleScript's text item delimiters |
| 071 | set AppleScript's text item delimiters to tab |
| 072 | set listSize to every text item of strStdOut |
| 073 | set AppleScript's text item delimiters to strDelim |
| 074 | set numWpx to (first item of listSize) as integer |
| 075 | set numHpx to (last item of listSize) as integer |
| 076 | log numWpx |
| 077 | log numHpx |
| 078 | end repeat |
| 079 |
|
| 080 | return |
| 081 |
|
| 082 |
|
| 083 |
|
| 084 |
|
| 085 | ######################## |
| 086 | #パスのエスケープ |
| 087 | to doPathEscape(strFilePath) |
| 088 | set strFilePath to strFilePath as text |
| 089 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 090 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 091 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 092 | |
| 093 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\")) |
| 094 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("$") withString:("\\$")) |
| 095 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("`") withString:("\\`")) |
| 096 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\"") withString:("\\\\\\\"")) |
| 097 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\"")) |
| 098 | |
| 099 | return ocidFilePath |
| 100 | end doPathEscape |