| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 | WEBLOCファイルをurlファイルとtxtファイルに |
| 007 | urlファイルをWEBLOCファイルとtxtファイルにします |
| 008 |
|
| 009 | V1 初回作成 |
| 010 | V1.1 ファイル重複時に数字を付与する方式に変更 |
| 011 |
|
| 012 | com.cocolog-nifty.quicktimer.icefloe *) |
| 013 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 014 | use AppleScript version "2.8" |
| 015 | use framework "Foundation" |
| 016 | use framework "AppKit" |
| 017 | use framework "UniformTypeIdentifiers" |
| 018 | use scripting additions |
| 019 |
|
| 020 | property refMe : a reference to current application |
| 021 |
|
| 022 | ######################## |
| 023 | # |
| 024 | # on run {listAliasFilePath} |
| 025 |
|
| 026 | (* |
| 027 | #テスト用ダイアログ |
| 028 | *) |
| 029 | on run |
| 030 | set appBundle to current application's NSBundle's bundleWithIdentifier:("com.apple.osax.standardadditions") |
| 031 | set strChooseAFile to (appBundle's localizedStringForKey:("Choose a File") value:("Choose a File") table:("Localizable")) as text |
| 032 | set appBundle to current application's NSBundle's bundleWithIdentifier:("com.apple.AppKit") |
| 033 | set strMultiple to (appBundle's localizedStringForKey:("NSMultipleDocuments") value:("NSMultipleDocuments") table:("AccessibilityImageDescriptions")) as text |
| 034 | set strMsg to ("ファイル選択 " & strMultiple & return & strChooseAFile & "") as text |
| 035 | set strPrompt to ("ファイル選択 " & strMultiple & return & strChooseAFile & "") as text |
| 036 | set aliasDesktopDirPath to (path to desktop from user domain) as alias |
| 037 | set listUTI to {"com.apple.web-internet-location", "com.microsoft.internet-shortcut"} as list |
| 038 | try |
| 039 | tell application "SystemUIServer" |
| 040 | activate |
| 041 | set listAliasFilePath to (choose file strMsg with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and multiple selections allowed without showing package contents) as list |
| 042 | end tell |
| 043 | on error strErrMes number numErrNo |
| 044 | log strErrMes & numErrNo |
| 045 | return false |
| 046 | end try |
| 047 | if listAliasFilePath is {} then |
| 048 | return false |
| 049 | end if |
| 050 | |
| 051 | |
| 052 | set boolDone to (open listAliasFilePath) |
| 053 | |
| 054 | return doQuitSelf() |
| 055 | end run |
| 056 |
|
| 057 |
|
| 058 | ######################## |
| 059 | #open |
| 060 | on open listAliasFilePath |
| 061 | |
| 062 | #単体で来たらリストにしておく |
| 063 | if (class of listAliasFilePath) is alias then |
| 064 | set listFilePath to {listAliasFilePath} as list |
| 065 | else if (class of listAliasFilePath) is file then |
| 066 | set listFilePath to {listAliasFilePath} as list |
| 067 | else if (class of listAliasFilePath) is list then |
| 068 | #リストならそのまま処理 |
| 069 | set listFilePath to listAliasFilePath as list |
| 070 | end if |
| 071 | tell application "Finder" |
| 072 | set listAliasFilePath to listAliasFilePath as alias list |
| 073 | end tell |
| 074 | |
| 075 | set listFileName to {} as list |
| 076 | repeat with itemAliasFilePath in listAliasFilePath |
| 077 | set aliasItemFilePath to itemAliasFilePath as alias |
| 078 | set recordInfoFor to (info for aliasItemFilePath) as record |
| 079 | set strExt to (name extension of recordInfoFor) as text |
| 080 | if strExt is "webloc" then |
| 081 | #URLにして |
| 082 | set strFilePath to (POSIX path of aliasItemFilePath) as text |
| 083 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 084 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 085 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 086 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 087 | # |
| 088 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 089 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
| 090 | # |
| 091 | set ocidURLString to doGetURL(ocidFilePathURL) |
| 092 | set ocidWinURLString to doMakeURLfile(ocidURLString) |
| 093 | set ocidSaveFileName to doChkWindowsFileName(ocidBaseFileName) |
| 094 | # |
| 095 | set ocidSaveBaseFileURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName) isDirectory:(false)) |
| 096 | #Windows用のインターネットショートカットファイル |
| 097 | set ocidSaveFilePathURL to (ocidSaveBaseFileURL's URLByAppendingPathExtension:("url")) |
| 098 | set ocidSaveFilePathURL to doChkFileExist(ocidSaveFilePathURL) |
| 099 | set listDone to (ocidWinURLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSShiftJISStringEncoding) |error|:(reference)) |
| 100 | if (first item of listDone) is false then |
| 101 | log (second item of listDone)'s localizedDescription() as text |
| 102 | return false |
| 103 | end if |
| 104 | #UTF8のテキスト |
| 105 | set ocidSaveFilePathURL to (ocidSaveBaseFileURL's URLByAppendingPathExtension:("URL.txt")) |
| 106 | set ocidSaveFilePathURL to doChkFileExist(ocidSaveFilePathURL) |
| 107 | set ocidTextURLString to doMakeText(ocidURLString) |
| 108 | set listDone to (ocidTextURLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
| 109 | if (first item of listDone) is false then |
| 110 | log (second item of listDone)'s localizedDescription() as text |
| 111 | return false |
| 112 | end if |
| 113 | else if strExt is "url" then |
| 114 | |
| 115 | #URLにして |
| 116 | set strFilePath to (POSIX path of aliasItemFilePath) as text |
| 117 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 118 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 119 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 120 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 121 | # |
| 122 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 123 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
| 124 | set ocidSaveBaseFileURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(ocidBaseFileName)) |
| 125 | # |
| 126 | set ocidURLString to doGetURLFile2URL(ocidFilePathURL) |
| 127 | |
| 128 | #UTF8のテキスト |
| 129 | set ocidSaveFilePathURL to (ocidSaveBaseFileURL's URLByAppendingPathExtension:("URL.txt")) |
| 130 | set ocidSaveFilePathURL to doChkFileExist(ocidSaveFilePathURL) |
| 131 | set ocidTextURLString to doMakeText(ocidURLString) |
| 132 | set listDone to (ocidURLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
| 133 | if (first item of listDone) is false then |
| 134 | log (second item of listDone)'s localizedDescription() as text |
| 135 | return false |
| 136 | end if |
| 137 | #WEBLOCデータ |
| 138 | set ocidWeblocData to doMakeWebloc(ocidURLString) |
| 139 | set ocidSaveFilePathURL to (ocidSaveBaseFileURL's URLByAppendingPathExtension:("webloc")) |
| 140 | set ocidSaveFilePathURL to doChkFileExist(ocidSaveFilePathURL) |
| 141 | set ocidOption to (refMe's NSDataWritingAtomic) |
| 142 | set listDone to (ocidWeblocData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)) |
| 143 | if (first item of listDone) is false then |
| 144 | log (second item of listDone)'s localizedDescription() as text |
| 145 | return false |
| 146 | end if |
| 147 | end if |
| 148 | end repeat |
| 149 | |
| 150 | return true |
| 151 | end open |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 | ######################## |
| 157 | # |
| 158 | to doChkFileExist(argFilePathURL) |
| 159 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 160 | set argFilePath to argFilePathURL's |path|() |
| 161 | set boolDirExists to appFileManager's fileExistsAtPath:(argFilePath) isDirectory:(false) |
| 162 | if (boolDirExists as boolean) is true then |
| 163 | set ocidExtensionName to argFilePath's pathExtension() |
| 164 | set ocidFileName to argFilePath's lastPathComponent() |
| 165 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
| 166 | set ocidConainerDirPath to argFilePath's stringByDeletingLastPathComponent() |
| 167 | set numCnt to 1 as integer |
| 168 | repeat |
| 169 | set numCnt to (numCnt + 1) as integer |
| 170 | set strNewFileName to ("" & ocidBaseFileName & space & numCnt & "." & ocidExtensionName & "") as text |
| 171 | set ocidNewFilePath to ocidConainerDirPath's stringByAppendingPathComponent:(strNewFileName) |
| 172 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidNewFilePath) isDirectory:(false) |
| 173 | if (boolDirExists as boolean) is false then |
| 174 | set argFilePathURL to refMe's NSURL's fileURLWithPath:(ocidNewFilePath) isDirectory:(false) |
| 175 | exit repeat |
| 176 | end if |
| 177 | end repeat |
| 178 | return argFilePathURL |
| 179 | |
| 180 | else if (boolDirExists as boolean) is false then |
| 181 | return argFilePathURL |
| 182 | end if |
| 183 | |
| 184 | |
| 185 | end doChkFileExist |
| 186 |
|
| 187 | ######################## |
| 188 | #WEBLOCデータを作る |
| 189 | to doMakeWebloc(argURLstring) |
| 190 | # |
| 191 | set ocidWeblocDict to refMe's NSMutableDictionary's alloc()'s init() |
| 192 | ocidWeblocDict's setObject:(argURLstring) forKey:("URL") |
| 193 | #PLIST2NSDATA(MutableContainersAndLeaves) |
| 194 | set ocidFromat to (refMe's NSPropertyListXMLFormat_v1_0) |
| 195 | set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidWeblocDict) format:(ocidFromat) options:0 |error|:(reference) |
| 196 | set ocidWeblocData to (first item of listResponse) |
| 197 | |
| 198 | return ocidWeblocData |
| 199 | |
| 200 | end doMakeWebloc |
| 201 |
|
| 202 |
|
| 203 | ######################## |
| 204 | #URLの取り出し |
| 205 | to doGetURLFile2URL(argFilePathURL) |
| 206 | # |
| 207 | set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(argFilePathURL) encoding:(refMe's NSShiftJISStringEncoding) |error|:(reference)) |
| 208 | if (first item of listResponse) is false then |
| 209 | set listReadStrings to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)) |
| 210 | set ocidReadStrings to (first item of listResponse) |
| 211 | end if |
| 212 | set ocidReadStrings to (first item of listResponse) |
| 213 | ## |
| 214 | set strCRLF to ("" & return & linefeed & "") as text |
| 215 | set strLF to ("" & linefeed & "") as text |
| 216 | set strCR to ("" & return & "") as text |
| 217 | set ocidReadStrings to (ocidReadStrings's stringByReplacingOccurrencesOfString:(strCRLF) withString:(strLF)) |
| 218 | set ocidReadStrings to (ocidReadStrings's stringByReplacingOccurrencesOfString:(strCR) withString:(strLF)) |
| 219 | # |
| 220 | set ocidStringArray to ocidReadStrings's componentsSeparatedByString:(strLF) |
| 221 | repeat with itemLine in ocidStringArray |
| 222 | set boolHasPre to (itemLine's hasPrefix:("URL=")) as boolean |
| 223 | if boolHasPre is true then |
| 224 | set ocidURLString to (itemLine's stringByReplacingOccurrencesOfString:("URL=") withString:("")) |
| 225 | exit repeat |
| 226 | end if |
| 227 | end repeat |
| 228 | |
| 229 | return ocidURLString |
| 230 | |
| 231 | end doGetURLFile2URL |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 | ######################## |
| 236 | # |
| 237 | to doMakeText(argURLstring) |
| 238 | set ocidURLString to refMe's NSMutableString's stringWithString:(argURLstring) |
| 239 | set strLF to ("" & linefeed & "") as text |
| 240 | ocidURLString's appendString:(strLF) |
| 241 | |
| 242 | return ocidURLString |
| 243 | |
| 244 | end doMakeText |
| 245 |
|
| 246 | ######################## |
| 247 | #doChkWindowsFileName |
| 248 | to doChkWindowsFileName(argFileName) |
| 249 | set ocidChkChar to refMe's NSCharacterSet's characterSetWithCharactersInString:("/:*?<>|") |
| 250 | set ocidFileNameArray to argFileName's componentsSeparatedByCharactersInSet:(ocidChkChar) |
| 251 | set ocidReturnFileName to ocidFileNameArray's componentsJoinedByString:("_") |
| 252 | # |
| 253 | set ocidChkChar to refMe's NSCharacterSet's characterSetWithCharactersInString:("\"") |
| 254 | set ocidFileNameArray to ocidReturnFileName's componentsSeparatedByCharactersInSet:(ocidChkChar) |
| 255 | set ocidReturnFileName to ocidFileNameArray's componentsJoinedByString:("_") |
| 256 | # |
| 257 | set ocidChkChar to refMe's NSCharacterSet's characterSetWithCharactersInString:("\\") |
| 258 | set ocidFileNameArray to ocidReturnFileName's componentsSeparatedByCharactersInSet:(ocidChkChar) |
| 259 | set ocidReturnFileName to ocidFileNameArray's componentsJoinedByString:("_") |
| 260 | # |
| 261 | return ocidReturnFileName |
| 262 | |
| 263 | end doChkWindowsFileName |
| 264 |
|
| 265 | ######################## |
| 266 | #doMakeURLfile |
| 267 | to doMakeURLfile(argURLstring) |
| 268 | |
| 269 | set strCRLF to ("" & return & linefeed & "") as text |
| 270 | set ocidURLString to refMe's NSMutableString's stringWithString:("[InternetShortcut]") |
| 271 | ocidURLString's appendString:(strCRLF) |
| 272 | ocidURLString's appendString:("URL=") |
| 273 | ocidURLString's appendString:(argURLstring) |
| 274 | ocidURLString's appendString:(strCRLF) |
| 275 | |
| 276 | return ocidURLString |
| 277 | |
| 278 | end doMakeURLfile |
| 279 |
|
| 280 | ######################## |
| 281 | #doGetURL |
| 282 | to doGetURL(argFilePathURL) |
| 283 | set listResponse to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(argFilePathURL) |error|:(reference) |
| 284 | if (item 2 of listResponse) = (missing value) then |
| 285 | set ocidWeblocDict to (item 1 of listResponse) |
| 286 | else if (item 2 of listResponse) ≠ (missing value) then |
| 287 | set strErrorNO to (item 2 of listResponse)'s code() as text |
| 288 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
| 289 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 290 | return false |
| 291 | end if |
| 292 | |
| 293 | set ocidURL to ocidWeblocDict's objectForKey:("URL") |
| 294 | # |
| 295 | set argFilePathURL to (missing value) |
| 296 | set ocidWeblocDict to (missing value) |
| 297 | return ocidURL |
| 298 | end doGetURL |
| 299 |
|
| 300 | ############################## |
| 301 | ############################## |
| 302 | to doQuitSelf() |
| 303 | set listBundleID to {"com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner"} as list |
| 304 | repeat with itemBundleID in listBundleID |
| 305 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID)) |
| 306 | repeat with itemApp in ocidAppArray |
| 307 | try |
| 308 | set boolDone to itemApp's terminate() |
| 309 | end try |
| 310 | end repeat |
| 311 | end repeat |
| 312 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 313 | try |
| 314 | with timeout of 5 seconds |
| 315 | tell application "System Events" to quit |
| 316 | end timeout |
| 317 | end try |
| 318 | return true |
| 319 | end doQuitSelf |
| 320 |
|