« launchctlのファイルをロックしてアップデート停止する | トップページ | [ドロップレット]ファイルパスを戻す »

クリップボードからファイルパスを取得(少し改良)


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application


########################
## クリップボードの中身取り出し
########################
###初期化
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
##中身の要素
set ocidPastBoardTypeArray to ocidPasteboard's types
#URL要素の確認
set boolFileName to ocidPastBoardTypeArray's containsObject:(refMe's NSFilenamesPboardType)
set boolFileURL to ocidPastBoardTypeArray's containsObject:("public.file-url")
set boolAppleURL to ocidPastBoardTypeArray's containsObject:("Apple URL pasteboard type")
#URL要素の取得
if boolFileName is true then
  ###NSFilenamesPboardTypeはUNIXパス形式
  set ocidFilePathArray to ocidPasteboard's propertyListForType:(refMe's NSFilenamesPboardType)
else if boolFileName is false then
  if boolFileURL is true then
    ##public.file-url の場合はNSURLのクラスを指定して取得した後でパス指定する
    set ocidClassesArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidClassesArrayM's addObject:((refMe's NSURL)'s class)
    set ocidFilePathURLArray to ocidPasteboard's readObjectsForClasses:(ocidClassesArrayM) options:(missing value)
    set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
    repeat with itemArray in ocidFilePathURLArray
      set ocidFilePath to itemArray's |path|
(ocidFilePathArray's addObject:(ocidFilePath))
    end repeat
  else if boolFileURL is false then
    ##Apple URL pasteboard typeにはURLは1つだけしか格納されない
    set ocidAppleURLArray to ocidPasteboard's propertyListForType:("Apple URL pasteboard type")
    set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
    try
      repeat with itemArray in ocidAppleURLArray
        if itemArray ≠ (missing value) then
          set ocidFilePathURL to (refMe's NSURL's URLWithString:(itemArray))
(ocidFilePathArray's addObject:(ocidFilePathURL's |path|))
        end if
      end repeat
    on error
      set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
(ocidFilePathArray's addObject:("ファイルのパスの取得に失敗しました"))
    end try
  else
    set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
(ocidFilePathArray's addObject:("ファイルのパスの取得に失敗しました"))
  end if
end if

########################
## 出力用のテキストにする
########################
set ocidOutPutString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
repeat with itemArray in ocidFilePathArray
(ocidOutPutString's appendString:(itemArray))
(ocidOutPutString's appendString:("\n"))
end repeat
set strOutPutString to ocidOutPutString as text

########################
## ダイアログ
########################
##前面に
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasIconPath to (POSIX file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns")
try
  set recordResult to (display dialog strOutPutString with title "戻り値です" default answer strOutPutString buttons {"クリップボードにコピー", "終了", "Finderに表示"} default button "Finderに表示" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
on error
return "エラーしました"
end try
if (gave up of recordResult) is true then
return "時間切れです"
end if
##############################
#####Finderに表示
##############################

if button returned of recordResult is "Finderに表示" then
  ##NSWorkspaceで
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set ocidOpenURLsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
  repeat with itemFilePath in ocidFilePathArray
    set ocidFilePath to itemFilePath's stringByStandardizingPath()
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath))
(ocidOpenURLsArray's addObject:(ocidFilePathURL))
  end repeat
  ##SelectingURLsを実行する
appSharedWorkspace's activateFileViewerSelectingURLs:(ocidOpenURLsArray)
  
end if
##############################
#####値のコピー
##############################
if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strText as text
    end tell
  end try
end if


return 0



|

« launchctlのファイルをロックしてアップデート停止する | トップページ | [ドロップレット]ファイルパスを戻す »

NSPasteboard」カテゴリの記事