
【Applescript】テキスト中の『ファイルパス』から、Finderでファイルパスの場所を選択したFinderWindowを開く(ファイルをOPENはしない、場所を知らせるだけ)
詳しくはこちら【Safari・FireFox用Script Editorで開く】 |
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | |
| 006 | ファイルリスト(改行区切り)を入力として |
| 007 | パスの内容をFinderで選択状態にして開きます |
| 008 | |
| 009 | |
| 010 | |
| 011 | com.cocolog-nifty.quicktimer.icefloe *) |
| 012 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 013 | use AppleScript version "2.8" |
| 014 | use framework "Foundation" |
| 015 | use framework "AppKit" |
| 016 | use framework "UniformTypeIdentifiers" |
| 017 | use scripting additions |
| 018 | |
| 019 | property refMe : a reference to current application |
| 020 | property strArgText : ("") as text |
| 021 | |
| 022 | |
| 023 | #on run {strArgText} |
| 024 | on run |
| 025 | if strArgText is "" then |
| 026 | ############################## |
| 027 | # ダイアログ |
| 028 | set strIconFilePath to ("/System/Library/PrivateFrameworks/AOSUI.framework/Versions/A/Resources/AppleID.icns") as text |
| 029 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 030 | set strMsg to ("パステキストを入力してください" & return & "Finderでパスの場所を選択します") as text |
| 031 | set strTitle to ("入力してください") as text |
| 032 | set strOK to ("実行") as text |
| 033 | set strCancel to ("キャンセル") as text |
| 034 | set listBotton to {strOK, strCancel} as list |
| 035 | set strDefaultAnswer to doGetPastBoard() |
| 036 | try |
| 037 | tell application "System Events" |
| 038 | activate |
| 039 | set recordResult to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listBotton default button strOK cancel button strCancel with icon aliasIconFilePath giving up after 20 without hidden answer) as record |
| 040 | end tell |
| 041 | on error strErrMsg number numErrNo |
| 042 | log strErrMsg & numErrNo |
| 043 | return false |
| 044 | end try |
| 045 | try |
| 046 | tell application "System Events" to quit |
| 047 | end try |
| 048 | set strResponseButton to (button returned of recordResult) as text |
| 049 | set boolGaveUp to (gave up of recordResult) as boolean |
| 050 | if boolGaveUp is true then |
| 051 | log "時間切れ" |
| 052 | return false |
| 053 | else if strResponseButton is strOK then |
| 054 | set strArgText to (text returned of recordResult) as text |
| 055 | end if |
| 056 | else |
| 057 | set strArgText to (first item of strArgText) as text |
| 058 | end if |
| 059 | |
| 060 | set boolDone to doOpenPath2Finder(strArgText) |
| 061 | set strArgText to "" as text |
| 062 | try |
| 063 | with timeout of 3 seconds |
| 064 | tell application "System Events" to quit |
| 065 | end timeout |
| 066 | on error strErrMes number numErrNo |
| 067 | log "Quit System Events" & strErrMes & numErrNo |
| 068 | return false |
| 069 | end try |
| 070 | |
| 071 | return return |
| 072 | end run |
| 073 | |
| 074 | |
| 075 | |
| 076 | to doOpenPath2Finder(strArgText) |
| 077 | log strArgText |
| 078 | # |
| 079 | set ocidArgText to refMe's NSString's stringWithString:(strArgText) |
| 080 | set ocidArgText to (ocidArgText's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(linefeed)) |
| 081 | set ocidArgText to (ocidArgText's stringByReplacingOccurrencesOfString:(return) withString:(linefeed)) |
| 082 | set ocidArgText to (ocidArgText's stringByReplacingOccurrencesOfString:(linefeed & linefeed) withString:(linefeed)) |
| 083 | set ocidArgText to (ocidArgText's stringByReplacingOccurrencesOfString:(tab) withString:(" ")) |
| 084 | # |
| 085 | set ocidLineArray to ocidArgText's componentsSeparatedByString:(linefeed) |
| 086 | repeat with itemString in ocidLineArray |
| 087 | |
| 088 | set strFilePath to itemString as text |
| 089 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 090 | set boolHasPre to (ocidFilePathStr's hasPrefix:("file:///")) as boolean |
| 091 | if boolHasPre is false then |
| 092 | set boolHasPrePath to (ocidFilePathStr's hasPrefix:("/")) as boolean |
| 093 | if boolHasPrePath is true then |
| 094 | set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping() |
| 095 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 096 | set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath() |
| 097 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath)) |
| 098 | set boolSetURL to true as boolean |
| 099 | else |
| 100 | set boolSetURL to false as boolean |
| 101 | end if |
| 102 | else if boolHasPre is true then |
| 103 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initWithString:(ocidFilePath)) |
| 104 | set boolSetURL to true as boolean |
| 105 | end if |
| 106 | if boolSetURL is true then |
| 107 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 108 | set ocidOpenURLsArray to refMe's NSMutableArray's alloc()'s init() |
| 109 | (ocidOpenURLsArray's addObject:(ocidFilePathURL)) |
| 110 | (appSharedWorkspace's activateFileViewerSelectingURLs:(ocidOpenURLsArray)) |
| 111 | delay 1 |
| 112 | end if |
| 113 | |
| 114 | end repeat |
| 115 | set strArgText to "" as text |
| 116 | return true |
| 117 | end doOpenPath2Finder |
| 118 | |
| 119 | |
| 120 | |
| 121 | ############################## |
| 122 | # クリップボードの中身取り出し |
| 123 | to doGetPastBoard() |
| 124 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
| 125 | set ocidPastBoardTypeArray to appPasteboard's types() |
| 126 | set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSStringPboardType) |
| 127 | if (boolContain as boolean) is true then |
| 128 | set ocidString to appPasteboard's stringForType:(refMe's NSStringPboardType) |
| 129 | set strDefaultAnswer to ocidString as text |
| 130 | else |
| 131 | set strDefaultAnswer to ("パス20程度まで") as text |
| 132 | end if |
| 133 | #ダイアログ保護のため50文字まで |
| 134 | set numCntChar to (count of every character of strDefaultAnswer) as integer |
| 135 | if numCntChar > 3000 then |
| 136 | set strDefaultAnswer to (text 1 through 3000 of strDefaultAnswer) as text |
| 137 | else |
| 138 | set strDefaultAnswer to (text 1 through numCntChar of strDefaultAnswer) as text |
| 139 | end if |
| 140 | log strDefaultAnswer |
| 141 | return strDefaultAnswer |
| 142 | end doGetPastBoard |
| AppleScriptで生成しました | |
