[メモ・Note]選択したメモをファイル出力(複数のメモを単一ファイルにまとめて出力)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # 選択したノートをテキストファイルに出力します |
005 | # 20250214 v1.1 リスト選択の排他を修正した |
006 | #com.cocolog-nifty.quicktimer.icefloe |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use scripting additions |
012 | property refMe : a reference to current application |
013 | |
014 | |
015 | |
016 | set listAccountName to {} as list |
017 | |
018 | tell application "Notes" |
019 | set listAccount to every account as list |
020 | set numAccount to (count of listAccount) as number |
021 | repeat with objAccount in listAccount |
022 | tell objAccount |
023 | set objAccountID to id |
024 | set strAccountName to name as text |
025 | end tell |
026 | copy "" & strAccountName & "" to end of listAccountName |
027 | end repeat |
028 | end tell |
029 | |
030 | if listAccount is {} then |
031 | return "アカウントがありません" |
032 | end if |
033 | |
034 | #####ダイアログを前面に |
035 | tell current application |
036 | set strName to name as text |
037 | end tell |
038 | if strName is "osascript" then |
039 | tell application "Finder" to activate |
040 | else |
041 | tell current application to activate |
042 | end if |
043 | |
044 | try |
045 | set strTitle to ("アカウントを選択してください") as text |
046 | set strPrompt to ("アカウントを選択してください") as text |
047 | set objResponse to (choose from list listAccountName with title strTitle with prompt strPrompt default items (item 1 of listAccountName) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) |
048 | on error |
049 | log "エラーしました" |
050 | return |
051 | end try |
052 | if objResponse is false then |
053 | return |
054 | end if |
055 | set strAccount to objResponse as text |
056 | |
057 | set listFolderName to {} as list |
058 | tell application "Notes" |
059 | ## show account strAccount with separately |
060 | tell account strAccount |
061 | set listFolderName to name of every folder |
062 | end tell |
063 | end tell |
064 | if listFolderName is {} then |
065 | return "フォルダがありません" |
066 | end if |
067 | #####ダイアログを前面に |
068 | tell current application |
069 | set strName to name as text |
070 | end tell |
071 | if strName is "osascript" then |
072 | tell application "Finder" to activate |
073 | else |
074 | tell current application to activate |
075 | end if |
076 | try |
077 | set strTitle to ("フォルダを選択してください") as text |
078 | set strPrompt to ("フォルダを選択してください") as text |
079 | set objResponse to (choose from list listFolderName with title strTitle with prompt strPrompt default items (item 1 of listFolderName) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) |
080 | on error |
081 | log "エラーしました" |
082 | return |
083 | end try |
084 | if objResponse is false then |
085 | return |
086 | end if |
087 | set strFolderName to objResponse as text |
088 | (* |
089 | tell application "Notes" |
090 | ## show account strAccount with separately |
091 | tell account strAccount |
092 | show folder strFolderName with separately |
093 | properties |
094 | tell folder strFolderName |
095 | properties |
096 | end tell |
097 | end tell |
098 | end tell |
099 | *) |
100 | set listNoteName to {} as list |
101 | tell application "Notes" |
102 | ## show account strAccount with separately |
103 | tell account strAccount |
104 | tell folder strFolderName |
105 | set listNoteName to name of every note |
106 | end tell |
107 | end tell |
108 | end tell |
109 | |
110 | if listNoteName is {} then |
111 | return "ノートがありません" |
112 | end if |
113 | |
114 | #####ダイアログを前面に |
115 | tell current application |
116 | set strName to name as text |
117 | end tell |
118 | if strName is "osascript" then |
119 | tell application "Finder" to activate |
120 | else |
121 | tell current application to activate |
122 | end if |
123 | try |
124 | set strTitle to ("ノートを選択してください") as text |
125 | set strPrompt to ("ノートを選択してください") as text |
126 | set objResponse to (choose from list listNoteName with title strTitle with prompt strPrompt default items (item 1 of listNoteName) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) |
127 | on error |
128 | log "エラーしました" |
129 | return |
130 | end try |
131 | if objResponse is false then |
132 | return |
133 | end if |
134 | set listNoteName to objResponse as list |
135 | ####出力用のテキスト |
136 | set ocidOutputString to refMe's NSMutableString's alloc()'s init() |
137 | set ocidOutputBodyString to refMe's NSMutableString's alloc()'s init() |
138 | # |
139 | set strHeader to ("<!DOCTYPE html>\n<html lang=\"ja\">\n<head>\n<meta charset=\"UTF-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n<title>" & strAccount & "</title>\n</head>\n<body>\n<header>\n<nav>\n</nav>フォルダー:" & strFolderName & "\n</header>\n<main>\n<section>") |
140 | (ocidOutputBodyString's appendString:(strHeader)) |
141 | |
142 | tell application "Notes" |
143 | # show account strAccount with separately |
144 | tell account strAccount |
145 | # show with separately |
146 | tell folder strFolderName |
147 | # show with separately |
148 | repeat with itemNoteName in listNoteName |
149 | tell note itemNoteName |
150 | properties |
151 | set strID to id as text |
152 | tell note id strID |
153 | # properties |
154 | end tell |
155 | set strName to name as text |
156 | set strBody to body as text |
157 | set strPlainText to plaintext as text |
158 | # |
159 | # (ocidOutputBodyString's appendString:(strName)) |
160 | # (ocidOutputBodyString's appendString:("<br>")) |
161 | (ocidOutputBodyString's appendString:(strBody)) |
162 | (ocidOutputBodyString's appendString:("<hr>")) |
163 | # |
164 | (ocidOutputString's appendString:(strName)) |
165 | (ocidOutputString's appendString:("\n")) |
166 | (ocidOutputString's appendString:(strPlainText)) |
167 | (ocidOutputString's appendString:("\n------\n")) |
168 | |
169 | end tell |
170 | end repeat |
171 | end tell |
172 | end tell |
173 | end tell |
174 | |
175 | |
176 | |
177 | set strDateno to doGetDateNo() |
178 | set strFooter to ("</section>\n</main>\n<footer>\n<p>" & strDateno & "日取得</p>\n</footer>\n</body>\n</html>") as text |
179 | (ocidOutputBodyString's appendString:(strFooter)) |
180 | |
181 | |
182 | #保存先 |
183 | set appFileManager to refMe's NSFileManager's defaultManager() |
184 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
185 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
186 | #パス |
187 | set ocidTextFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("ノート出力.txt") isDirectory:(false) |
188 | set ocidBodyFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("ノート出力.html") isDirectory:(false) |
189 | #保存 |
190 | set listDone to ocidOutputString's writeToURL:(ocidTextFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
191 | |
192 | set listDone to ocidOutputBodyString's writeToURL:(ocidBodyFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
193 | |
194 | |
195 | return |
196 | |
197 | ################################ |
198 | # 日付 doGetDateNo() |
199 | ################################ |
200 | to doGetDateNo() |
201 | ###今日 |
202 | set ocidDate to (current application's NSDate's |date|()) |
203 | ###フォーマット初期化 |
204 | set ocidFormatterJP to current application's NSDateFormatter's alloc()'s init() |
205 | ###日本のカレンダー |
206 | set ocidCalendarID to (current application's NSCalendarIdentifierJapanese) |
207 | set ocidCalendarJP to current application's NSCalendar's alloc()'s initWithCalendarIdentifier:(ocidCalendarID) |
208 | ###東京タイムゾーン |
209 | set ocidTimezoneJP to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo") |
210 | ###日本語 |
211 | set ocidLocaleJP to current application's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP_POSIX") |
212 | ###フォーマットをセット |
213 | ocidFormatterJP's setTimeZone:(ocidTimezoneJP) |
214 | ocidFormatterJP's setLocale:(ocidLocaleJP) |
215 | ocidFormatterJP's setCalendar:(ocidCalendarJP) |
216 | ocidFormatterJP's setDateStyle:(current application's NSDateFormatterFullStyle) |
217 | ocidFormatterJP's setDateFormat:("GGyy") |
218 | ###今日の日付にフォーマットを適応 |
219 | set ocidDateStringEra to ocidFormatterJP's stringFromDate:(ocidDate) |
220 | ## ocidFormatterJP's setDateFormat:("年MM月dd日EEEE") |
221 | ocidFormatterJP's setDateFormat:("年MM月dd日EEEE a hh時mm分ss秒") |
222 | set ocidDateString to ocidFormatterJP's stringFromDate:(ocidDate) |
223 | set strDateAndTime to ((ocidDateStringEra as text) & (ocidDateString as text)) as text |
224 | ###テキストで戻す |
225 | return strDateAndTime |
226 | end doGetDateNo |
AppleScriptで生成しました |
| 固定リンク