« [plist]ユーザーの言語設定を取得する | トップページ | ペーストボード書き出し(考え中) »

[Stickies]クリップボードからステッキーズを生成する(仮)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# 
#com.cocolog-nifty.quicktimer.icefloe
# 新規でステッキーズを生成します 色や文言はカスタマイズして利用してください
(*
v2 ZOrderの値を修正した


*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
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()
#
tell application id "com.apple.Stickies"
quit
end tell
#
################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
####タイプを取得
set ocidPastBoardTypeArray to ocidPasteboard's types()
log ocidPastBoardTypeArray as list
set boolContainPng to ocidPastBoardTypeArray's containsObject:"public.png"
set boolContainText to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
set boolContainRtf to ocidPastBoardTypeArray's containsObject:"public.rtf"
set boolContainTiff to ocidPastBoardTypeArray's containsObject:"public.tiff"

########################
##
########################
#保存先ディレクトリ
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
set ocidSaveDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers/com.apple.Stickies/Data/Library/Stickies") isDirectory:(true)
###処理の分岐
if boolContainRtf is true then
  ##RTFの場合はそのまま
  set ocidAttarStringArray to ocidPasteboard's readObjectsForClasses:({refMe's NSAttributedString}) options:(missing value)
  set ocidAttarString to ocidAttarStringArray's firstObject()
  set ocidRange to refMe's NSMakeRange(0, (ocidAttarString's |length|))
  set ocidAttarData to ocidAttarString's RTFFromRange:(ocidRange) documentAttributes:(missing value)
  set ocidUUIDString to doSaveAttarData(ocidAttarData, ocidSaveDirPathURL)
  
else if boolContainText is true then
  ##テキストの場合はダイアログを出す
  set strReadString to doGetPasteboard()
  set aliasIconPath to doGetAppIconAliasFilePath("com.apple.Stickies")
  #前面に
  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 strMes to ("メッセージ") as text
  set recordResult to (display dialog strMes with title strTitle default answer strReadString buttons {"キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 30 with icon aliasIconPath without hidden answer)
  if (gave up of recordResult) is true then
return "時間切れです"
  else if (button returned of recordResult) is "キャンセル" then
return "キャンセルです"
  else
    set strSetValue to (text returned of recordResult) as text
  end if
  ####
  set ocidAttarData to doMakeAttarText(strSetValue)
  set ocidUUIDString to doSaveAttarData(ocidAttarData, ocidSaveDirPathURL)
  
else if boolContainPng is true then
  ##画像の場合
  set strSaveImageFileName to ("Attach.png") as text
  set ocidImageData to ocidPasteboard's dataForType:(refMe's NSPasteboardTypePNG)
  set ocidUUIDString to doSaveImageAttarData(ocidImageData, ocidSaveDirPathURL, strSaveImageFileName)
  
else if boolContainTiff is true then
  set strSaveImageFileName to ("Attach.tif") as text
  set ocidImageData to ocidPasteboard's dataForType:(refMe's NSPasteboardTypeTIFF)
  set ocidUUIDString to doSaveImageAttarData(ocidImageData, ocidSaveDirPathURL, strSaveImageFileName)
else
return "キャンセル"
end if



################################

#PLIST 設定ファイルの読み込み
set ocidPlistFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(".SavedStickiesState") isDirectory:(false)
set ocidPlistArray to refMe's NSMutableArray's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL)
set numCntArray to (count of ocidPlistArray) as integer
#追加するDICT=新しいステっキーズのデータ
set ocidAddDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
#数値系 前後関係のINDEX
set numSetValue to (numCntArray + 1) as integer
ocidAddDict's setValue:(numSetValue) forKey:("ZOrder")
#開いているのが1 閉じているのが0
set numSetValue to (0) as integer
ocidAddDict's setValue:(numSetValue) forKey:("ExpandFrameY")
#日本語スペルチェックは無いのでデフォルト値
set numSetValue to (8455) as integer
ocidAddDict's setValue:(numSetValue) forKey:("SpellCheckingTypes")
#テキスト系
ocidAddDict's setValue:(ocidUUIDString) forKey:("UUID")
#開いた時のサイズ
ocidAddDict's setValue:("{300, 200}") forKey:("ExpandedSize")
#ポジション 左上原点 xy wh
ocidAddDict's setValue:("{{30, 335}, {300, 200}}") forKey:("Frame")
#BOOL系
set ocidBool to (refMe's NSNumber's numberWithBool:false)
ocidAddDict's setValue:(ocidBool) forKey:("Translucent")
set ocidBool to (refMe's NSNumber's numberWithBool:true)
ocidAddDict's setValue:(ocidBool) forKey:("Floating")
#色データはテキスト形式のDICTを個別で作成 
#ControlColor
set ocidSetObjectDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set numSetValue to (0.859) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Red")
set numSetValue to (0.772) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Green")
set numSetValue to (0.012) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Blue")
set numSetValue to (1) as integer
ocidSetObjectDict's setValue:(numSetValue) forKey:("Alpha")
ocidAddDict's setObject:(ocidSetObjectDict) forKey:("ControlColor")
#HighlightColor
set ocidSetObjectDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set numSetValue to (0.737) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Red")
set numSetValue to (0.662) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Green")
set numSetValue to (0.007) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Blue")
set numSetValue to (1) as integer
ocidSetObjectDict's setValue:(numSetValue) forKey:("Alpha")
ocidAddDict's setObject:(ocidSetObjectDict) forKey:("HighlightColor")
#SpineColor
set ocidSetObjectDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set numSetValue to (0.997) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Red")
set numSetValue to (0.917) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Green")
set numSetValue to (0.239) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Blue")
set numSetValue to (1) as integer
ocidSetObjectDict's setValue:(numSetValue) forKey:("Alpha")
ocidAddDict's setObject:(ocidSetObjectDict) forKey:("SpineColor")
#StickyColor
set ocidSetObjectDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set numSetValue to (0.996) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Red")
set numSetValue to (0.956) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Green")
set numSetValue to (0.611) as number
ocidSetObjectDict's setValue:(numSetValue) forKey:("Blue")
set numSetValue to (1) as integer
ocidSetObjectDict's setValue:(numSetValue) forKey:("Alpha")
ocidAddDict's setObject:(ocidSetObjectDict) forKey:("StickyColor")
#作成した設定を読み込んだPLITに追加
ocidPlistArray's addObject:(ocidAddDict)
#SavedStickiesStateはテキスト形式のXMLなのでXML形式で保存
set ocidNSbplist to refMe's NSPropertyListXMLFormat_v1_0
set listPlistData to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistArray) format:ocidNSbplist options:0 |error|:(reference)
set ocidPlistEditData to (item 1 of listPlistData)
###この場合は上書き
set listDone to ocidPlistEditData's writeToURL:(ocidPlistFilePathURL) options:0 |error|:(reference)
if (item 1 of listDone) is false then
return "保存に失敗しました"
end if

delay 0.5
tell application id "com.apple.Stickies"
activate
end tell


########################
## イメージの保存とRTFの生成
########################
on doSaveImageAttarData(argImageData, argSaveDirPathURL, argSaveImageFileName)
  
  ##RTFを生成
  set strRTFString to ("{\\rtf1\\ansi\\ansicpg932\\cocoartf2761{\\NeXTGraphic Attach.png \\appleattachmentpadding0}}") as text
  set ocidRTFString to refMe's NSString's stringWithString:(strRTFString)
  set ocidAttarData to ocidRTFString's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
  #UUID=rtfdファイル名=ディレクトリ名
  set ocidConcreteUUID to refMe's NSUUID's UUID()
  set ocidUUIDString to ocidConcreteUUID's UUIDString()
  #ファイル名は固定
  set strFileName to ("TXT.rtf") as text
  #rtfdファイル=ディレクトリのURL
  set ocidUUIDDirPathURL to argSaveDirPathURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(false)
  set ocidRtfdDirPathURL to ocidUUIDDirPathURL's URLByAppendingPathExtension:("rtfd")
  #ディレクトリを作る アクセス権755
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions)
  set appFileManager to refMe's NSFileManager's defaultManager()
  set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidRtfdDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
  #RTFデータの保存先
  set ocidSaveRTFPathURL to ocidRtfdDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false)
  #イメージデータの保存先
  set ocidSaveImagePathURL to ocidRtfdDirPathURL's URLByAppendingPathComponent:(argSaveImageFileName) isDirectory:(false)
  #保存
  set ocidOption to (refMe's NSDataWritingAtomic)
  #RTF
  set listDone to ocidAttarData's writeToURL:(ocidSaveRTFPathURL) options:(ocidOption) |error|:(reference)
  if (item 1 of listDone) is false then
return "保存に失敗しました"
  end if
  #イメージファイル
  set listDone to argImageData's writeToURL:(ocidSaveImagePathURL) options:(ocidOption) |error|:(reference)
  if (item 1 of listDone) is false then
return "保存に失敗しました"
  end if
return ocidUUIDString
end doSaveImageAttarData

########################
## Attarテキストの保存
########################
on doSaveAttarData(argAttarData, argSaveDirPathURL)
  #UUID=rtfdファイル名=ディレクトリ名
  set ocidConcreteUUID to refMe's NSUUID's UUID()
  set ocidUUIDString to ocidConcreteUUID's UUIDString()
  #ファイル名は固定
  set strFileName to ("TXT.rtf") as text
  
  #rtfdファイル=ディレクトリのURL
  set ocidUUIDDirPathURL to argSaveDirPathURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(false)
  set ocidRtfdDirPathURL to ocidUUIDDirPathURL's URLByAppendingPathExtension:("rtfd")
  #ディレクトリを作る アクセス権755
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions)
  set appFileManager to refMe's NSFileManager's defaultManager()
  set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidRtfdDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
  #RTFデータの保存先
  set ocidSaveRTFPathURL to ocidRtfdDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false)
  #アトリビュートテキストのDATAを保存
  set ocidOption to (refMe's NSDataWritingAtomic)
  set listDone to argAttarData's writeToURL:(ocidSaveRTFPathURL) options:(ocidOption) |error|:(reference)
  if (item 1 of listDone) is false then
return "保存に失敗しました"
  end if
return ocidUUIDString
end doSaveAttarData

########################
## RTFデータの作成
########################
on doMakeAttarText(argText)
  #アトリビュートテキストを生成する
  set ocidAttarString to refMe's NSMutableAttributedString's alloc()'s initWithString:(argText)
  #セットするDICT
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
  #フォントサイズ
  #フォントファミリー指定はスタイル入りで
  set ocidFont to refMe's NSFont's fontWithName:("BIZ UDPGothic Bold") |size|:(18)
  #システムフォント指定
  #set ocidFont to refMe's NSFont's boldSystemFontOfSize:(18)
ocidAttrDict's setObject:(ocidFont) forKey:(refMe's NSFontAttributeName)
  #色指定
  #set ocidSetColor to refMe's NSColor's colorWithDisplayP3Red:1.0 green:0 blue:0 alpha:1.0
  #set ocidSetColor to refMe's NSColor's colorWithCalibratedRed:0.044 green:0.378 blue:0.673 alpha:1.0
  set ocidSetColor to refMe's NSColor's grayColor()
ocidAttrDict's setObject:(ocidSetColor) forKey:(refMe's NSForegroundColorAttributeName)
  #
  set ocidRange to refMe's NSMakeRange(0, (ocidAttarString's |length|))
ocidAttarString's addAttributes:(ocidAttrDict) range:(ocidRange)
  
  set ocidAttarData to ocidAttarString's RTFFromRange:(ocidRange) documentAttributes:(missing value)
return ocidAttarData
end doMakeAttarText
########################
## クリップボードの中身取り出し
########################
on doGetPasteboard()
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidPastBoardTypeArray to appPasteboard's types
  ###テキストがあれば
  set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
  if boolContain = true then
    ###値を格納する
    tell application "Finder"
      set strReadString to (the clipboard as text) as text
    end tell
    ###Finderでエラーしたら
  else
    set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
    if boolContain = true then
      set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
      set strReadString to ocidReadString as text
    else
log "テキストなし"
      set strReadString to "入力してください" as text
    end if
  end if
return strReadString
end doGetPasteboard


########################
#AppIconのパスを求める
########################
on doGetAppIconAliasFilePath(argBundleID)
  #
  set appFileManager to refMe's NSFileManager's defaultManager()
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(argBundleID))
  if ocidAppBundle ≠ (missing value) then
    set ocidAppPathURL to ocidAppBundle's bundleURL()
  else if ocidAppBundle = (missing value) then
    set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
  end if
  if ocidAppBundle ≠ (missing value) then
    set ocidAppPathURL to ocidAppBundle's bundleURL()
  else if ocidAppBundle = (missing value) then
    set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID))
  end if
  if ocidAppPathURL = (missing value) then
    tell application "Finder"
      try
        set aliasAppApth to (application file id strBundleID) as alias
      on error
return "アプリケーションが見つかりませんでした"
      end try
    end tell
    set strAppPath to POSIX path of aliasAppApth as text
    set strAppPathStr to refMe's NSString's stringWithString:(strAppPath)
    set strAppPath to strAppPathStr's stringByStandardizingPath()
    set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true
  end if
  ####ダイアログに指定アプリのアイコンを表示する
  ###アイコン名をPLISTから取得
  set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
  set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
  set strIconFileName to (ocidPlistDict's valueForKey:("CFBundleIconFile")) as text
  ###ICONのURLにして
  set strPath to ("Contents/Resources/" & strIconFileName) as text
  set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false
  ###拡張子の有無チェック
  set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text
  if strExtensionName is "" then
    set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns"
  end if
  ##-->これがアイコンパス
log ocidIconFilePathURL's absoluteString() as text
  ###ICONファイルが実際にあるか?チェック
  set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePathURL's |path|)
  ###ICONがみつかない時用にデフォルトを用意する
  if boolExists is false then
    set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
  else
    set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias
  end if
return aliasIconPath
end doGetAppIconAliasFilePath


|

« [plist]ユーザーの言語設定を取得する | トップページ | ペーストボード書き出し(考え中) »

AppleScript NSPasteboard」カテゴリの記事