« [Stickies]クリップボードからステッキーズを生成する(仮) | トップページ | PDF417バーコードを生成する »

ペーストボード書き出し(考え中)


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

#!/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 "AppKit"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
################################
#レコード
set recordUTI2Type to {|com.apple.webarchive|:("com.apple.webarchive"), |public.png|:(refMe's NSPasteboardTypePNG), |public.url|:(refMe's NSPasteboardTypeURL), |public.tiff|:("public.tiff"), |public.rtf|:(refMe's NSPasteboardTypeRTF), |com.adobe.pdf|:(refMe's NSPasteboardTypePDF), |public.utf8-plain-text|:(refMe's NSPasteboardTypeString), |public.tab-separated-values-text|:(refMe's NSPasteboardTypeTabularText), |public.file-url|:(refMe's NSPasteboardTypeFileURL), |public.html|:(refMe's NSPasteboardTypeHTML), |com.apple.icns|:("com.apple.icns")} as record
set ocidUTI2TypeDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidUTI2TypeDict's setDictionary:(recordUTI2Type)
#
set recordUTI2Class to {|com.apple.webarchive|:(refMe's NSData), |public.png|:(refMe's NSData), |com.apple.icns|:(refMe's NSData), |public.tiff|:(refMe's NSData), |public.rtf|:(refMe's NSData), |com.adobe.pdf|:(refMe's NSData), |public.utf8-plain-text|:(refMe's NSString), |public.tab-separated-values-text|:(refMe's NSString), |public.url|:(refMe's NSURL), |public.file-url|:(refMe's NSURL), |public.html|:(refMe's NSString)} as record
set ocidUTI2ClassDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidUTI2ClassDict's setDictionary:(recordUTI2Class)
#
set recordUTI2Extension to {|com.apple.webarchive|:("webarchive"), |com.apple.icns|:"icns", |public.png|:"png", |public.tiff|:"tif", |public.rtf|:"rtf", |com.adobe.pdf|:"pdf", |public.utf8-plain-text|:"txt", |public.tab-separated-values-text|:"tsv", |public.url|:"webloc", |public.file-url|:"fileloc", |public.html|:"html"} as record
set ocidUTI2ExtensionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidUTI2ExtensionDict's setDictionary:(recordUTI2Extension)
#
set ocidAllKeysArray to ocidUTI2ExtensionDict's allKeys()
#set listAllKeysArray to ocidAllKeysArray as list
################################
###ペーストボードを取得
set appNSPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to appNSPasteboard's types()
log ocidPastBoardTypeArray as list
#ペーストボードのデータタイプのみ処理するための処理
set ocidPasteboardClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
repeat with itemArray in ocidPastBoardTypeArray
  set boolContain to (ocidAllKeysArray's containsObject:(itemArray))
  if boolContain is true then
(ocidPasteboardClassArray's addObject:(itemArray))
  end if
end repeat
set listAllKeysArray to ocidPasteboardClassArray as list

##############################
###ダイアログを前面に出す
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 strTitle to "選んでください" as text
set strPrompt to "複数選択時はコマンドキーをおしながら" as text
try
  set listResponse to (choose from list listAllKeysArray with title strTitle with prompt strPrompt default items (item 1 of listAllKeysArray) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
  error "エラーしました" number -200
end try
if listResponse = {} then
log "何も選択していない"
  # 止めるなら ここに returnを入れる
else if (item 1 of listResponse) is false then
return "キャンセルしました"
  error "キャンセルしました" number -200
else
  ##############################
  ###データの保存先
  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDocumentDirPathURL to ocidURLsArray's firstObject()
  #
  set strSetValue to doGetDateNo("yyyyMMdd_HHmmss") as text
  set strSetValue to ("SavePasteboard/" & strSetValue) as text
  set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:(strSetValue)
  #
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
  # 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
  set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
  set aliasSaveDirPath to (ocidSaveDirPathURL's absoluteURL()) as alias
  ######
  repeat with itemResponse in listResponse
    set strUTI to itemResponse as text
    set ocidClass to (ocidUTI2TypeDict's objectForKey:(strUTI))
    set ocidClassesArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
(ocidClassesArray's addObject:(ocidClass))
    set ocidReadData to (appNSPasteboard's dataForType:(ocidClass))
log className() of ocidReadData as text
    set strExtension to (ocidUTI2ExtensionDict's valueForKey:(strUTI)) as text
    set strSaveFileName to (strUTI & "." & strExtension) as text
log strUTI
log strExtension
    set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName))
    
    if strUTI contains "url" then
      set ocidClass to (ocidUTI2ClassDict's objectForKey:(strUTI))'s class
      set ocidClassArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
(ocidClassArray's addObject:(ocidClass))
      set ocidReadDataArray to (appNSPasteboard's readObjectsForClasses:(ocidClassArray) options:(missing value))
      set ocidURL to ocidReadDataArray's firstObject()
      set strURL to (ocidURL's absoluteString()) as text
      set ocidPlistDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPlistDict's setValue:(strURL) forKey:("URL"))
      set ocidFromat to refMe's NSPropertyListXMLFormat_v1_0
      set listPlistEditDataArray to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFromat) options:0 |error|:(reference))
      set ocidPlistData to item 1 of listPlistEditDataArray
log doSaveData(ocidPlistData, ocidSaveFilePathURL)
    else
      
      # log doSaveData(ocidReadData, ocidSaveFilePathURL)
      # else if strUTI is "public.png" then
      # log doSaveData(ocidReadData, ocidSaveFilePathURL)
      # else if strUTI is "com.adobe.pdf" then
      # log doSaveData(ocidReadData, ocidSaveFilePathURL)
      # else if strUTI is "public.rtf" then
      # log doSaveData(ocidReadData, ocidSaveFilePathURL)
      # else if strUTI is "public.html" then
      # log doSaveData(ocidReadData, ocidSaveFilePathURL)
      # else if strUTI is "public.tab-separated-values-text" then
      # log doSaveData(ocidReadData, ocidSaveFilePathURL)
      # else if strUTI is "public.utf8-plain-text" then
log doSaveData(ocidReadData, ocidSaveFilePathURL)
      
    end if
    
  end repeat
end if
tell application "Finder"
open location aliasSaveDirPath
end tell


return


########################
#データの保存
on doSaveData(argData, argSaveFilePathURL)
  #保存
  set ocidOption to (refMe's NSDataWritingAtomic)
  set listDone to argData's writeToURL:(argSaveFilePathURL) options:(ocidOption) |error|:(reference)
  if (item 1 of listDone) is false then
log "保存に失敗しました"
return false
  else
return true
  end if
end doSaveData


##############################
### 今の日付日間 テキスト
to doGetDateNo(argDateFormat)
  ####日付情報の取得
  set ocidDate to current application's NSDate's |date|()
  ###日付のフォーマットを定義
  set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
  set ocidTimeZone to refMe's NSTimeZone's alloc()'s initWithName:"Asia/Tokyo"
ocidNSDateFormatter's setTimeZone:(ocidTimeZone)
ocidNSDateFormatter's setDateFormat:(argDateFormat)
  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
  set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo



return


|

« [Stickies]クリップボードからステッキーズを生成する(仮) | トップページ | PDF417バーコードを生成する »

AppleScript NSPasteboard」カテゴリの記事