AppleScript RTF

[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
delay 0.5
#表示するテキスト
set strSetValue to ("新しいステッキーズ") as text
#アトリビュートテキストを生成する
set ocidAttarString to refMe's NSMutableAttributedString's alloc()'s initWithString:(strSetValue)
#セットする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)
#UUID=rtfdファイル名=ディレクトリ名
set ocidConcreteUUID to refMe's NSUUID's UUID()
set ocidUUIDString to ocidConcreteUUID's UUIDString()
#ファイル名は固定
set strFileName to ("TXT.rtf") as text
#保存先ディレクトリ
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)
#rtfdファイル=ディレクトリのURL
set ocidUUIDDirPathURL to ocidSaveDirPathURL'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 listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidRtfdDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
#RTFデータの保存先
set ocidSaveDilePathURL to ocidRtfdDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false)
#アトリビュートテキストのDATAを保存
set ocidOption to (refMe's NSDataWritingAtomic)
set listDone to ocidAttarData's writeToURL:(ocidSaveDilePathURL) options:(ocidOption) |error|:(reference)
if (item 1 of listDone) is false then
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


|

[NSHTMLTextDocumentType]ペーストボードのリッチテキストデータをHTMLファイルに書き出す

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

property refMe : a reference to current application

################################
##### パス関連
################################
set strFilePath to "~/Desktop/クリップボードのRTF書類のHTML.html" as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false


################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
###NSDataとしてRTFデータを受け取る
set ocidReadPasteboardRTF to ocidPasteboard's dataForType:(refMe's NSPasteboardTypeRTF)

################################
##### HTML変換
################################
###NSAttributedStringに変換
set ocidAttributedString to refMe's NSAttributedString's alloc()'s initWithRTF:ocidReadPasteboardRTF documentAttributes:(missing value)
###文字数カウント
set numStringLenge to ocidAttributedString's |length|() as integer
###アトリビュートをHTMLに変換
set ocidAttributedDict to refMe's NSDictionary's dictionaryWithObject:(refMe's NSHTMLTextDocumentType) forKey:(refMe's NSDocumentTypeDocumentAttribute)
####NSDATAに変換
set listDocAttar to ocidAttributedString's dataFromRange:{location:0, |length|:numStringLenge} documentAttributes:ocidAttributedDict |error|:(reference)
####取り出して
set ocidHTMLData to item 1 of listDocAttar
################################
##### 保存
################################
#####保存
set boolFileWrite to (ocidHTMLData's writeToURL:ocidFilePathURL atomically:true)


|

[NSPasteboardTypeRTF]ペーストボードのリッチテキストデータをRTFファイルに書き出す

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

property refMe : a reference to current application

################################
##### パス関連
################################
set strFilePath to "~/Desktop/クリップボードのRTF書類.rtf" as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false


################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
###NSDataとしてRTFデータを受け取る
set ocidReadPasteboardRTF to ocidPasteboard's dataForType:(refMe's NSPasteboardTypeRTF)

################################
##### 保存
################################
####ファイルに書き出し
set boolFileWrite to (ocidReadPasteboardRTF's writeToURL:ocidFilePathURL atomically:true)


|

[textutil]ブランク(空)のRTFリッチテキスト書類を作成する(変換する)

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

property refMe : a reference to current application

################################
##### パス関連
################################

set strFilePath to "~/Desktop/空のテキスト書類.txt" as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false

################################
##### 空のテキスト
################################
####空のString
set strBlankText to ""
set ocidBlankText to refMe's NSString's stringWithString:strBlankText
################################
##### まずはテキストファイルとして保存
################################
####ファイルに書き出し
set boolFileWrite to (ocidBlankText's writeToURL:ocidFilePathURL atomically:false encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))

################################
##### RTFのファイルパス
################################

set strFilePath to "~/Desktop/空のRTF書類.rtf" as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidRtfFilePath to ocidFilePathStr's stringByStandardizingPath()


##########################################
####リッチテキスト書類に変換
##########################################
do shell script "/usr/bin/textutil -convert rtf \"" & (ocidFilePath as text) & "\"  -output \"" & (ocidRtfFilePath as text) & "\""



|

[NSString]ブランク(空)のRTFリッチテキスト書類を作成する(やっつけ)

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

#!/usr/bin/env osascript

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


set strFilePath to "~/Desktop/空のリッチテキスト.rtf"
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
set aliasFilePath to ocidFilePathURL as «class furl»

set strRTFcode to "{¥rtf1¥ansi¥ansicpg932¥cocoartf2708}"


open for access aliasFilePath with write permission
write strRTFcode to aliasFilePath
close access aliasFilePath

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

property refMe : a reference to current application

################################
##### パス関連
################################
set strFilePath to "~/Desktop/空のRTF書類.rtf" as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false

################################
##### NSStringでやっつける方法
################################
####空のString
set strBlankRtfContents to "{\\rtf1\\ansi\\ansicpg932\\cocoartf2708}"
set ocidBlankRtfContents to refMe's NSString's stringWithString:strBlankRtfContents
################################
##### 保存
################################
####ファイルに書き出し
set boolFileWrite to (ocidBlankRtfContents's writeToURL:ocidFilePathURL atomically:false encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))



|

[RTFFromRange]ブランク(空)のRTFリッチテキスト書類を作成する

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

property refMe : a reference to current application

################################
##### パス関連
################################
set strFilePath to "~/Desktop/空のRTF書類.rtf" as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false

################################
##### NSAttributedString関連
################################
####空のNSAttributedString
set ocidBlankStrings to refMe's NSAttributedString's alloc()'s initWithString:""
####NSdataにRTFとして保存
set ocidBlankRTFData to ocidBlankStrings's RTFFromRange:{location:0, |length|:0} documentAttributes:(missing value)

################################
##### 保存
################################
####ファイルに書き出し
set boolFileWrite to (ocidBlankRTFData's writeToURL:ocidFilePathURL atomically:true)


|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom