| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | python3版 |
| 006 | ちょっとエスケープに工夫した |
| 007 |
|
| 008 |
|
| 009 | モジュールは、ご自身のpy環境にあわせて実行してください |
| 010 | モジュールインストールローカルインストール(ローカル=要管理者権限) |
| 011 | /usr/bin/sudo /usr/bin/python3 -m pip install --upgrade --no-warn-script-location opencv-python |
| 012 |
|
| 013 | ユーザーインストール(ユーザー=管理者権限不要) |
| 014 | /usr/bin/python3 -m pip install --upgrade --user --no-warn-script-location opencv-python |
| 015 |
|
| 016 | com.cocolog-nifty.quicktimer.icefloe *) |
| 017 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 018 | use AppleScript version "2.8" |
| 019 | use framework "Foundation" |
| 020 | use scripting additions |
| 021 | property refMe : a reference to current application |
| 022 |
|
| 023 |
|
| 024 | ############################# |
| 025 | ###入力ファイル |
| 026 | ############################# |
| 027 | tell current application |
| 028 | set strName to name as text |
| 029 | end tell |
| 030 | if strName is "osascript" then |
| 031 | tell application "Finder" to activate |
| 032 | else |
| 033 | tell current application to activate |
| 034 | end if |
| 035 | #デスクトップ |
| 036 | set aliasDesktopDirPath to (path to desktop from user domain) as alias |
| 037 | set listUTI to {"public.movie"} as list |
| 038 | set strTitle to ("ファイル選択") as text |
| 039 | set strPrompt to ("ムービーファイルを選んでください") as text |
| 040 | tell application "SystemUIServer" |
| 041 | activate |
| 042 | 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 |
| 043 | end tell |
| 044 |
|
| 045 | #NSURLのArrayを作成して |
| 046 | set ocidFileURLArrayM to refMe's NSMutableArray's alloc()'s init() |
| 047 |
|
| 048 | repeat with itemAliasFilePath in listAliasFilePath |
| 049 | set aliasFilePath to itemAliasFilePath as alias |
| 050 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 051 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 052 | set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping() |
| 053 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 054 | set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath() |
| 055 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 056 | (ocidFileURLArrayM's addObject:(ocidFilePathURL)) |
| 057 | end repeat |
| 058 |
|
| 059 | #ソート |
| 060 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:") |
| 061 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
| 062 | set ocidFileURLArrayS to ocidFileURLArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
| 063 |
|
| 064 | #Arrayのアイテム数 |
| 065 | set numCntArray to ocidFileURLArrayS's |count|() |
| 066 |
|
| 067 | repeat with itemIntNo from 0 to (numCntArray - 1) by 1 |
| 068 | #開くムービーのエイリアス・パスをエイリアスリストから取得して |
| 069 | set ocidItemFileURL to (ocidFileURLArrayS's objectAtIndex:(itemIntNo)) |
| 070 | set ocidItemFile to ocidItemFileURL's |path|() |
| 071 | set ocidItemFileENC to doPathEscape(ocidItemFile) |
| 072 | #SWIFT |
| 073 | set strPyCode to ("#!/usr/bin/env python3\n#coding: utf-8\nimport cv2\nstr_file_path = \\\"" & ocidItemFileENC & "\\\"\ncap_video = cv2.VideoCapture(str_file_path)\nbool_is_opened = cap_video.isOpened()\nif not bool_is_opened:\n print(\\\"動画ファイルを開けませんでした\\\")\nelse:\n float_width = cap_video.get(cv2.CAP_PROP_FRAME_WIDTH)\n float_height = cap_video.get(cv2.CAP_PROP_FRAME_HEIGHT)\n int_width = int(float_width)\n int_height = int(float_height)\n cap_video.release()\n print(f\\\"{int_width}\\t{int_height}\\\")") as text |
| 074 | #実行する |
| 075 | set strCmd to ("/usr/bin/python3 -c \"" & strPyCode & "\"") |
| 076 | set strStdOut to (do shell script strCmd) as text |
| 077 | |
| 078 | set strDelim to AppleScript's text item delimiters |
| 079 | set AppleScript's text item delimiters to tab |
| 080 | set listSize to every text item of strStdOut |
| 081 | set AppleScript's text item delimiters to strDelim |
| 082 | set numWpx to (first item of listSize) as integer |
| 083 | set numHpx to (last item of listSize) as integer |
| 084 | log numWpx |
| 085 | log numHpx |
| 086 | end repeat |
| 087 |
|
| 088 | return |
| 089 |
|
| 090 |
|
| 091 |
|
| 092 |
|
| 093 | ######################## |
| 094 | #パスのエスケープ |
| 095 | to doPathEscape(strFilePath) |
| 096 | set strFilePath to strFilePath as text |
| 097 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 098 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 099 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 100 | |
| 101 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\\") withString:("\\\\\\")) |
| 102 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("$") withString:("\\$")) |
| 103 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("`") withString:("\\`")) |
| 104 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("\"") withString:("\\\\\\\"")) |
| 105 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\"")) |
| 106 | |
| 107 | return ocidFilePath |
| 108 | end doPathEscape |