| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 | cpdfが別途必要です |
| 006 | <https://github.com/coherentgraphics/cpdf-binaries> |
| 007 | -impose <pagesize> |
| 008 | -impose-xy <x> <y> |
| 009 | -impose-columns |
| 010 | -impose-rtl |
| 011 | -impose-btt |
| 012 | -impose-margin <margin> |
| 013 | -impose-spacing <spacing> |
| 014 | -impose-linewidth <width> |
| 015 | -fast |
| 016 | cpdfが未インストールの場合インストールします |
| 017 | 困る場合は実行しないでください |
| 018 | cpdfインストール先は |
| 019 | /Users/ユーザーID/Library/Application Support/bin/cpdf |
| 020 | v1 初回作成 |
| 021 | v1.1 インストール機能をつけた |
| 022 | com.cocolog-nifty.quicktimer.icefloe *) |
| 023 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 024 | use AppleScript version "2.8" |
| 025 | use framework "Foundation" |
| 026 | use framework "UniformTypeIdentifiers" |
| 027 | use framework "AppKit" |
| 028 | use scripting additions |
| 029 | property refMe : a reference to current application |
| 030 | ################## |
| 031 | #cpdfバイナリーのパス |
| 032 | set strResponse to doChkInstall("cpdf") |
| 033 | if strResponse is false then |
| 034 | set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text |
| 035 | else |
| 036 | set strBinPath to strResponse as text |
| 037 | end if |
| 038 | set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath) |
| 039 | set ocidBinPath to ocidBinPathStr's stringByStandardizingPath() |
| 040 | set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false) |
| 041 | #メタ文字 |
| 042 | set strLF to ("" & linefeed & "") as text |
| 043 | set strCR to ("" & return & "") as text |
| 044 | set strCRLF to ("" & return & linefeed & "") as text |
| 045 | set strSpace to ("" & space & "") as text |
| 046 | set strTab to ("" & tab & "") as text |
| 047 |
|
| 048 | ################## |
| 049 | #入力ファイル |
| 050 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 051 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 052 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 053 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 054 | #ダイアログを全面に |
| 055 | set strName to (name of current application) as text |
| 056 | if strName is "osascript" then |
| 057 | tell application "Finder" to activate |
| 058 | else |
| 059 | tell current application to activate |
| 060 | end if |
| 061 | set listUTI to {"com.adobe.pdf"} |
| 062 | set strMes to ("ファイルを選んでください") as text |
| 063 | set strPrompt to ("ファイルを選んでください") as text |
| 064 | try |
| 065 | ### ファイル選択時 |
| 066 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
| 067 | on error |
| 068 | log "エラーしました" |
| 069 | return "エラーしました" |
| 070 | end try |
| 071 | ################## |
| 072 | #入力ファイルパス |
| 073 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 074 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 075 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 076 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
| 077 | ################## |
| 078 | #出力ファイル |
| 079 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 080 | set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text |
| 081 | set strExtensionName to (ocidFilePathURL's pathExtension()) as text |
| 082 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 083 | set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias |
| 084 | # |
| 085 | set strDefaultFileName to (strBaseFileName & "-4in1.pdf") as text |
| 086 | # |
| 087 | set strPromptText to "名前を決めてください" as text |
| 088 | set strMesText to "名前を決めてください" as text |
| 089 | ###ファイル名 ダイアログ |
| 090 | tell application "SystemUIServer" |
| 091 | activate |
| 092 | set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strDefaultFileName with prompt strPromptText) as «class furl» |
| 093 | end tell |
| 094 | ################## |
| 095 | #インストールチェック |
| 096 | set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false) |
| 097 | if boolExist is false then |
| 098 | set boolDone to doInstall() |
| 099 | if boolDone is false then |
| 100 | log "インストールに失敗しました" |
| 101 | return false |
| 102 | end if |
| 103 | end if |
| 104 | ################## |
| 105 | #出力パス |
| 106 | set strSaveFilePath to (POSIX path of aliasFilePath) as text |
| 107 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
| 108 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
| 109 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
| 110 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 111 | ###拡張子 |
| 112 | set strExtension to (ocidFilePathURL's pathExtension()) as text |
| 113 | ###最後のアイテムがファイル名 |
| 114 | set strFileName to (ocidFilePathURL's lastPathComponent()) as text |
| 115 | ###拡張子のつけ忘れ対策 |
| 116 | if strFileName does not contain strExtensionName then |
| 117 | set strFileName to (strFileName & "." & strExtensionName) as text |
| 118 | set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName) |
| 119 | end if |
| 120 | ################## |
| 121 | #コマンド整形 |
| 122 | set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text |
| 123 | set strFilePath to (ocidFilePathURL's |path|()) as text |
| 124 | set strBinPath to (ocidBinFilePathURL's |path|()) as text |
| 125 | # |
| 126 | set strCommandText to ("\"" & strBinPath & "\" -impose-xy \"2 2\" -impose-rtl -impose-columns \"" & strFilePath & "\" -o \"" & strSaveFilePath & "\"") as text |
| 127 | log strCommandText |
| 128 | do shell script strCommandText |
| 129 | ############################## |
| 130 | #すでにインストール済みで |
| 131 | #ユーザーがRC設定している場合は |
| 132 | #パスが通るのでこれがエラーにならない |
| 133 | to doChkInstall(argBinName) |
| 134 | try |
| 135 | set strCmd to ("/bin/zsh -c 'where " & argBinName & "'") |
| 136 | set strResponse to (do shell script strCmd) as text |
| 137 | return strResponse |
| 138 | on error strErrMsg number numErrNo |
| 139 | return false |
| 140 | end try |
| 141 | end doChkInstall |
| 142 | ############################## |
| 143 | to doQuitSelf() |
| 144 | set strOutputString to (missing value) |
| 145 | set listAliasFilePath to (missing value) |
| 146 | set listBundleID to {"com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner"} as list |
| 147 | repeat with itemBundleID in listBundleID |
| 148 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID)) |
| 149 | repeat with itemApp in ocidAppArray |
| 150 | try |
| 151 | set boolDone to itemApp's terminate() |
| 152 | end try |
| 153 | end repeat |
| 154 | end repeat |
| 155 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 156 | try |
| 157 | tell application "System Events" to quit |
| 158 | end try |
| 159 | return true |
| 160 | end doQuitSelf |
| 161 | ############################## |
| 162 | #インストールサプ |
| 163 | to doInstall() |
| 164 | ############################## |
| 165 | #設定項目 |
| 166 | #ダウンロードURL |
| 167 | set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text |
| 168 | #インストール先 |
| 169 | set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text |
| 170 | ############################## |
| 171 | #メタ文字 |
| 172 | set strLF to ("" & linefeed & "") as text |
| 173 | set strCR to ("" & return & "") as text |
| 174 | set strCRLF to ("" & return & linefeed & "") as text |
| 175 | set strSpace to ("" & space & "") as text |
| 176 | set strTab to ("" & tab & "") as text |
| 177 | ############################## |
| 178 | #ダウンロード先(起動時に削除される項目) |
| 179 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 180 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
| 181 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
| 182 | set ocidUUIDString to ocidUUID's UUIDString |
| 183 | set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
| 184 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
| 185 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
| 186 | set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 187 | set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false) |
| 188 | set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text |
| 189 | set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text |
| 190 | ############################## |
| 191 | #保存先 最終的なインストール先 |
| 192 | set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir) |
| 193 | set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath() |
| 194 | set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false) |
| 195 | |
| 196 | ############################## |
| 197 | #今あるものをゴミ箱へ |
| 198 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true) |
| 199 | if (boolDirExists as boolean) is true then |
| 200 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 201 | set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 202 | end if |
| 203 | |
| 204 | ############################## |
| 205 | #新たにフォルダを作り直す |
| 206 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 207 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 208 | ############################## |
| 209 | #リダイレクト先 |
| 210 | set strCmd to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text |
| 211 | try |
| 212 | set strRedirectURL to (do shell script strCmd) as text |
| 213 | on error strErrMsg number numErrNo |
| 214 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 215 | return false |
| 216 | end try |
| 217 | ############################## |
| 218 | #ダウンロード |
| 219 | set strCmd to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text |
| 220 | try |
| 221 | set strResponse to (do shell script strCmd) as text |
| 222 | on error strErrMsg number numErrNo |
| 223 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 224 | return false |
| 225 | end try |
| 226 | ############################## |
| 227 | #解凍 |
| 228 | set strCmd to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text |
| 229 | try |
| 230 | set strResponse to (do shell script strCmd) as text |
| 231 | on error strErrMsg number numErrNo |
| 232 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 233 | return false |
| 234 | end try |
| 235 | ############################## |
| 236 | #解凍結果 |
| 237 | set reocdSystemInfot to (system info) as record |
| 238 | set strCPU to (CPU type of reocdSystemInfot) as text |
| 239 | #CPUで分岐 |
| 240 | if strCPU contains "ARM" then |
| 241 | #ARM |
| 242 | set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false) |
| 243 | |
| 244 | else |
| 245 | #INTEL |
| 246 | set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-Intel/cpdf") isDirectory:(false) |
| 247 | |
| 248 | end if |
| 249 | |
| 250 | #最終的なバイナリーの移動先 |
| 251 | set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false) |
| 252 | #移動 |
| 253 | set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference)) |
| 254 | ############################## |
| 255 | #解凍前のファイルをゴミ箱へ |
| 256 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 257 | set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference)) |
| 258 | |
| 259 | return true |
| 260 | |
| 261 | end doInstall |