AppleScript Notes

[Notes]選択したメモを別アカウントに複製(新規作成)します

macOS標準のメモアプリ(Notes.app)は
AppleScript上で複製出来ないので
同じ物をAppleScriptを利用して複製する場合は
同じ内容の物を別のアカウントに新規作成することになります

ダウンロード - e981b8e68a9ee38197e3819fe3838ee383bce38388e5908ce38197e38299e38282e381aee38292e588a5e381aee382a2e382abe382a6e383b3e38388e381abe4bd9ce68890e38199e3828b.scpt.zip


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

####ログを出す場合コメントアウトを外してね
(*
tell application "Script Editor"
tell application "System Events"
keystroke "l" using {command down, option down}
end tell
end tell
delay 1
*)
###選択中のメモを取得
tell application "Notes"
set listSelected to selection
end tell

#####移動先のアカウントを決めてもらう
##アカウントリストを初期化
set listAccountName to {} as list

tell application "Notes"
###アカウントリストを取得
set listAccount to every account as list
set numAccount to (count of listAccount) as number
repeat with objAccount in listAccount
##アカウントを順番に
tell objAccount
###アカウントIDを取得
set objAccountID to id
###アカウント名を取得
set strAccountName to name as text
end tell
###アカウントリストに格納する
copy "" & strAccountName & "" to end of listAccountName
end repeat
end tell

####選択ダイアログを出して
try
set objResponse to (choose from list listAccountName with title "移動先を選択してください" with prompt "移動先先のアカウントを選んでください" default items (item 1 of listAccountName) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed)
on error
log "エラーしました"
return
end try
if objResponse is false then
log "キャンセルしました"
return
end if

set strResponse to objResponse as text

tell application "Notes"
##選択したノートを順番に処理
repeat with objNote in listSelected
###まずは選択したオプジェクトについて
tell objNote
###コンテナ(ここではFolder)を取得
set objFolderName to container
##選択ノートのFolderについて
tell objFolderName
###選択ノートのフォルダ名を取得
set strFolderName to name
###選択ノートのフォルダのコンテナ(ここではAccount
set objAccount to container
###選択ノートのアカウントについて
tell objAccount
###選択ノートのアカウント名を取得
set strAccountrName to name
end tell
end tell
##メモの名前を取得
set strNoteName to name
####本文を取得
#注意
#ボディは『取得』できますが
#ほとんど書き込み出来ません
#これは回避方法がわからなかった
set objBody to body
##添付ファイルを取得『無い場合は{}
set objAttach to every attachment
#plaintextはリードオンリーなので不要
##set objPlaintext to plaintext
end tell
####移動先に同名のフォルダがあるか?確認
set boolFolderExists to exists of folder strFolderName of account strResponse
###フォルダがなければフォルダを作成する
if boolFolderExists is false then
##移動先のアカウント
tell account strResponse
##同名のフォルダを作る
make new folder with properties {name:strFolderName}
end tell
end if
##選択したのアカウント
tell account strResponse
##同名フォルダに新規ノートを作成
set objNewNote to make new note at (folder strFolderName) with properties {name:strNoteName, body:objBody}
end tell
###添付ファイルの処理
set numCntAttach to count objAttach
set numChkCnt to 1
##元のメモに添付のある数だけ処理
repeat numCntAttach times
set objAttachFile to item numChkCnt of objAttach
##添付ファイルのIDを取得
set numAttachID to id of objAttachFile
tell attachment id numAttachID
##パスを取得
set aliasAttachContents to contents
log aliasAttachContents
end tell
##新規添付ファイルを元のパスと同じ物を新規作成したメモに紐づける
make new attachment at objNewNote with data aliasAttachContents
set numChkCnt to numChkCnt + 1
end repeat

end repeat
end tell

|

[Notes]macOSメモアプリの基本

macOS標準のメモアプリ(Notes.app)の
AppleScript的基本構造

tell application "Notes"
-----------------添付ファイルはアカウント等に紐づかない
tell attachment
-->添付されたファイルここ
-->ローカル、クラウドに実態がある
end tell

-----------------ノート(メモ)は
--アカウント-->フォルダ-->ノートの階層構造
tell account
tell folder
tell note
-->ここにメモの本文がある
end tell
end tell
end tell
end tell

なので 各項目で一覧を取得すには こんな感じになります
tell application "Notes"
-----------------添付ファイルはアカウント等に紐づかない
set listAttach to every attachment
tell attachment
-->添付されたファイルここ
-->ローカル、クラウドに実態がある
end tell

-----------------ノート(メモ)は
--アカウント-->フォルダ-->ノートの階層構造
set listAccount to every account
tell account id XXXX
set listFolder to every folder
tell folder id XXXX
set listNote to every note
tell note id XXXX
-->ここにメモの本文がある
end tell
end tell
end tell
end tell

添付ファイルを取得する場合のサンプル

tell application "Notes"
##全ての添付ファイルを取得
set listAttach to every attachment
###添付ファイルのここに処理
repeat with objAttach in listAttach
set numID to id of objAttach
##添付ファイルIDについて
tell attachment id numID
##ファイルのエイリアス
set strAttachContainer to contents
log strAttachContainer
##ファイル名
set strAttachName to name
log strAttachName
##URL
set strAttachURL to URL
log strAttachURL
end tell
end repeat
end tell

|

[Notes]選択したノートを別のアカウントに移動します

macOS標準のメモアプリ(Notes.app)で
選択したメモ(note)を別のアカウントに移動します
同名のフォルダへ移動します。
同名のフォルダが無い場合は同名のフォルダを作ります。


ダウンロード - e981b8e68a9ee38197e3819fe3838ee383bce38388e38292e7a7bbe58b95e38199e3828b.scpt.zip


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

####ログを出す場合コメントアウトを外してね
(*
tell application "Script Editor"
tell application "System Events"
keystroke "l" using {command down, option down}
end tell
end tell
delay 1
*)



tell application "Notes"
set listSelected to selection
end tell

#####移動先のアカウントを決めてもらう
##アカウントリストを初期化
set listAccountName to {} as list

tell application "Notes"
###アカウントリストを取得
set listAccount to every account as list
set numAccount to (count of listAccount) as number
repeat with objAccount in listAccount
##アカウントを順番に
tell objAccount
###アカウントIDを取得
set objAccountID to id
###アカウント名を取得
set strAccountName to name as text
end tell
###アカウントリストに格納する
copy "" & strAccountName & "" to end of listAccountName
end repeat
end tell

####選択ダイアログを出して
try
set objResponse to (choose from list listAccountName with title "移動先を選択してください" with prompt "移動先先のアカウントを選んでください" default items (item 1 of listAccountName) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed)
on error
log "エラーしました"
return
end try
if objResponse is false then
log "キャンセルしました"
return
end if

set strResponse to objResponse as text

tell application "Notes"
##選択したノートを順番に処理
repeat with objNote in listSelected
###まずは選択したオプジェクトについて
tell objNote
###コンテナ(ここではFolder)を取得
set objFolderName to container
##選択ノートのFolderについて
tell objFolderName
###選択ノートのフォルダ名を取得
set strFolderName to name
###選択ノートのフォルダのコンテナ(ここではAccount
set objAccount to container
###選択ノートのアカウントについて
tell objAccount
###選択ノートのアカウント名を取得
set strAccountrName to name
end tell
end tell
end tell
####移動先に同名のフォルダがあるか?確認
set boolFolderExists to exists of folder strFolderName of account strResponse
###フォルダがなければフォルダを作成する
if boolFolderExists is false then
##移動先のアカウント
tell account strResponse
##同名のフォルダを作る
make new folder with properties {name:strFolderName}
end tell
end if
#####移動します
move objNote to folder strFolderName of account strResponse
end repeat
end tell

|

[Notes]個別のnoteはAppleScriptで複製(duplicate)出来ない

macOS標準のメモアプリ(Notes.app)の個別のメモ(note)は
複製出来ません。
移動は出来ます

ダウンロード - e3838ee383bce38388e381afduplicatee587bae69da5e381aae38184.scpt.zip

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

tell application "Script Editor"
tell application "System Events"
keystroke "l" using {command down, option down}
end tell
end tell
delay 1

tell application "Notes"

set listSelected to selection

set objSelect to item 1 of listSelected
end tell

tell application "Notes"
set objNote to objSelect
tell objNote
set strNoteName to name
set objBody to body
set objPlaintext to plaintext
end tell
duplicate objNote to folder "ID" of account "このMac"
-->error "Notesでエラーが起きました: Notes can not be copied." number -1717
end tell

|

[defaults] com.apple.Notes

~/Library/Containers には『メモ』フォルダが2つあるが
$HOMEa/Library/Containers/com.apple.Notes →こちらが本体
$HOME/Library/Containers/com.apple.Notes.SharingExtension
初期化時に間違えないように

この部分をOFFにします

20220203-125347

ダウンロード - com.apple.notes.zip



--Notesが起動中な場合は一旦終了
tell application "Finder"

tell application "System Events"
set listAppProcess to every application process
end tell
tell application "System Events"
set listAppProcess to name of every application process as list

repeat with objAppProcess in listAppProcess
set objAppProcess to objAppProcess as text
if objAppProcess is "Notes" then
tell application id "com.apple.Notes"
quit saving yes
end tell
end if
end repeat

end tell

end tell
delay 2

--設定を書き込む
set theComandText to ("defaults write com.apple.Notes ShouldPerformTextReplacement -bool false") as text
do shell script theComandText
set theComandText to ("defaults write com.apple.Notes ShouldUseSmartCopyPaste -bool false") as text
do shell script theComandText
set theComandText to ("defaults write com.apple.Notes ShouldUseSmartDashes -bool false") as text
do shell script theComandText
set theComandText to ("defaults write com.apple.Notes ShouldUseSmartLinks -bool false") as text
do shell script theComandText
set theComandText to ("defaults write com.apple.Notes ShouldCorrectSpellingAutomatically -bool false") as text
do shell script theComandText
set theComandText to ("defaults write com.apple.Notes ShouldUseSmartQuotes -bool false") as text
do shell script theComandText
set theComandText to ("defaults write com.apple.Notes ShouldContinuouslyCheckSpelling -bool false") as text
do shell script theComandText



sh

#!/bin/zsh

#

#

#

local thePsPrint=`ps -alx | grep Notes.app | grep -v "grep"| awk '{print $2}' `

echo $thePsPrint


if [ -n "$thePsPrint" ]

then


kill $thePsPrint


fi


defaults write com.apple.Notes ShouldPerformTextReplacement -bool false

defaults write com.apple.Notes ShouldUseSmartCopyPaste -bool false

defaults write com.apple.Notes ShouldUseSmartDashes -bool false

defaults write com.apple.Notes ShouldUseSmartLinks -bool false

defaults write com.apple.Notes ShouldCorrectSpellingAutomatically -bool false

defaults write com.apple.Notes ShouldUseSmartQuotes -bool false

defaults write com.apple.Notes ShouldContinuouslyCheckSpelling -bool false


sleep 2

open /System/Applications/Notes.app/

exit

|

その他のカテゴリー

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