WEBページの選択範囲(テキスト限定)からフラグメントを作成してURLに整形する
2:スクリプトを実行する
3:戻り値がテキストフラグメント付きのURLになります
1:WEBページ上のテキストを選択した状態でコピー(コマンド+C)

2:スクリプトを実行する
スクリプトメニューに入れておくと良いですね

3:戻り値がテキストフラグメント付きのURLになります

このように
#のフラグメントから『:~:text=』をキーにしてValueが%エンコードされたキーワードになります
https://www.google.com/advanced_search?hl=ja#:~:text=%E6%A4%9C%E7%B4%A2%E3%82%AA%E3%83%97%E3%82%B7%E3%83%A7%E3%83%B3
LINK

リンク先がこのようにハイライトされた状態になります
が
対象のページがCSS設定していないと意図した
表示にならないこともあります
仕様としては
https://example.com#:~:text=[prefix-,]textStart[,textEnd][,-suffix]
開始ワード ハイライトされる文書 終了ワード
といった形式で
指定することができるので
同じページに複数、対象のワードが出る場合は
開始ワード、終了ワード形式でないと
最初にマッチした部分にハイライトが入ります
ソース | |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | Microsoft Edgeの全画面で |
005 | 範囲選択したテキストをコピーします |
006 | そのコピーした範囲の |
007 | テキストフラグメントURLを生成して |
008 | クリップボードまたはWEBロケーションファイルとして保存します |
009 | |
010 | 仕様は |
011 | https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Fragment/Text_fragments |
012 | |
013 | とりあえず作った感じで |
014 | 仕様通りなのか?もアレな |
015 | v1 20250503 初回作成 |
016 | |
017 | com.cocolog-nifty.quicktimer.icefloe *) |
018 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
019 | use AppleScript version "2.8" |
020 | use framework "Foundation" |
021 | use framework "AppKit" |
022 | use framework "UniformTypeIdentifiers" |
023 | use scripting additions |
024 | |
025 | property refMe : a reference to current application |
026 | |
027 | ################################ |
028 | ######ペーストボードを取得 |
029 | ################################ |
030 | set ocidPasteboard to refMe's NSPasteboard's generalPasteboard() |
031 | #タイプを取得 |
032 | set ocidPastBoardTypeArray to ocidPasteboard's types() |
033 | log ocidPastBoardTypeArray as list |
034 | # |
035 | set boolContain to ocidPastBoardTypeArray's containsObject:("org.chromium.source-url") |
036 | if (boolContain as boolean) is false then |
037 | set refResponse to doDisplayAlert("テキストフラグメントURLが含まれていないのでURLを作成できません") |
038 | return "テキストフラグメントURLが含まれていないのでURLを作成できません" |
039 | else if (boolContain as boolean) is true then |
040 | #含まれているのでデータを取得して |
041 | set ocidPastBoardData to (ocidPasteboard's dataForType:("org.chromium.source-url")) |
042 | #テキストにします |
043 | set ocidURLstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
044 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLstring) |
045 | #クエリーアイテムに分解しておいて |
046 | set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(false) |
047 | set ocidFlagment to ocidURLomponents's fragment() |
048 | #すでにフラグメントがあるURLが入力の可能性があるから |
049 | #テキストフラグメントは再セットが基本 |
050 | #フラグメントになるテキスト |
051 | set ocidFlagMentString to refMe's NSMutableString's stringWithString:(":~:text=") |
052 | #テキストを取得して |
053 | set ocidPastBoardData to (ocidPasteboard's dataForType:("public.utf8-plain-text")) |
054 | set ocidContentsString to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
055 | #フラグメント用テキストに追加する |
056 | ocidFlagMentString's appendString:(ocidContentsString) |
057 | ocidURLomponents's setFragment:(ocidFlagMentString) |
058 | ##URLに戻して テキストにしておく |
059 | set ocidSetURL to ocidURLomponents's |URL|() |
060 | set ocidSetURLString to ocidSetURL's absoluteString() |
061 | #2重改行を取り除いておく |
062 | set ocidSetURLString to (ocidSetURLString's stringByReplacingOccurrencesOfString:("%0A%0A") withString:("%0A")) |
063 | set boolConteinReturn to ocidSetURLString's hasSuffix:("%0A") |
064 | if (boolConteinReturn as boolean) is true then |
065 | set ocidLength to ocidSetURLString's |length|() |
066 | set ocidSetURLString to ocidSetURLString's substringToIndex:(ocidLength - 3) |
067 | end if |
068 | end if |
069 | set strSetURL to ocidSetURLString as text |
070 | ######################## |
071 | ##ダイアログ |
072 | ######################## |
073 | set strIconPath to ("/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/InternetLocation.icns") as text |
074 | set aliasIconPath to (POSIX file strIconPath) as alias |
075 | #前面に |
076 | set strName to (name of current application) as text |
077 | if strName is "osascript" then |
078 | tell application "SystemUIServer" to activate |
079 | else |
080 | tell current application to activate |
081 | end if |
082 | # |
083 | set strTitle to ("テキストフラグメントURL\r戻り値です") as text |
084 | set strMes to ("\r\rテキストフラグメントURL\r戻り値です\n値のコピーができます\r『OPENして確認』でもクリップボードにURLが入ります\r") as text |
085 | tell application "SystemUIServer" |
086 | activate |
087 | set recordResult to (display dialog strMes with title strTitle default answer strSetURL buttons {"クリップボードにコピー", "ローケーションファイルにする", "OPENして確認"} default button "クリップボードにコピー" giving up after 30 with icon aliasIconPath without hidden answer) |
088 | end tell |
089 | if (gave up of recordResult) is true then |
090 | return "時間切れです" |
091 | else if (button returned of recordResult) is "OPENして確認" then |
092 | set strURL to (text returned of recordResult) as text |
093 | set ocidURLstring to refMe's NSString's stringWithString:(strURL) |
094 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLstring) |
095 | set boolDone to doSendPasteboard(strURL) as boolean |
096 | #デフォルトのブラウザで開く |
097 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
098 | set boolDone to appSharedWorkspace's openURL:(ocidURL) |
099 | else if button returned of recordResult is "クリップボードにコピー" then |
100 | set strReturnedText to (text returned of recordResult) as text |
101 | set boolDone to doSendPasteboard(strReturnedText) as boolean |
102 | if boolDone is false then |
103 | log ("ペーストボードへのコピーに失敗しました") as text |
104 | return "ペーストボードへのコピーに失敗しました" |
105 | end if |
106 | else if button returned of recordResult is "ローケーションファイルにする" then |
107 | set strURL to (text returned of recordResult) as text |
108 | log doMakeWebloc(strURL) |
109 | set boolDone to doSendPasteboard(strURL) as boolean |
110 | else |
111 | log (text returned of recordResult) as text |
112 | end if |
113 | |
114 | return |
115 | ######################## |
116 | ## WEBLOCファイルで保存 |
117 | ######################## |
118 | to doMakeWebloc(argURL) |
119 | set ocidURLstring to refMe's NSString's stringWithString:(argURL) |
120 | #WEBLOC用DICTにして |
121 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init() |
122 | (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL")) |
123 | #PLISTに |
124 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
125 | set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference)) |
126 | set ocidPlistData to (item 1 of listResponse) |
127 | #Window用のURLファイル |
128 | set strURLfileString to ("[InternetShortcut]\r\nURL=" & (argURL as text) & "") as text |
129 | set ocidURLfileString to refMe's NSString's alloc()'s initWithString:(strURLfileString) |
130 | #保存先はダウンロードフォルダ |
131 | set appFileManager to refMe's NSFileManager's defaultManager() |
132 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)) |
133 | set ocidDownloadsDirPathURL to ocidURLsArray's firstObject() |
134 | #ファイル名とパスを定義して |
135 | set strWeblocFileName to ("org.chromium.source-url.webloc") |
136 | set strURLFileName to ("org.chromium.source-url.url") |
137 | set ocidWeblocFilePathURL to (ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strWeblocFileName) isDirectory:(false)) |
138 | set ocidURLFilePathURL to (ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strURLFileName) isDirectory:(false)) |
139 | #WEBLOC保存 |
140 | set ocidOption to (refMe's NSDataWritingAtomic) |
141 | set listDone to ocidPlistData's writeToURL:(ocidWeblocFilePathURL) options:(ocidOption) |error|:(reference) |
142 | #URL保存 |
143 | set listDone to ocidURLfileString's writeToURL:(ocidURLFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
144 | #保存先を開く |
145 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
146 | set boolDone to appSharedWorkspace's openURL:(ocidDownloadsDirPathURL) |
147 | |
148 | end doMakeWebloc |
149 | |
150 | |
151 | ######################## |
152 | ## クリップボードに送る |
153 | ######################## |
154 | on doSendPasteboard(argText) |
155 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
156 | set ocidText to (refMe's NSString's stringWithString:(argText)) |
157 | appPasteboard's clearContents() |
158 | set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
159 | if boolDone = false then |
160 | try |
161 | tell application "Finder" |
162 | set the clipboard to strTitle as text |
163 | end tell |
164 | on error |
165 | return false |
166 | end try |
167 | else if boolDone = true then |
168 | return true |
169 | end if |
170 | end doSendPasteboard |
171 | |
172 | |
173 | |
174 | ################################ |
175 | ##エラーアラート |
176 | ################################ |
177 | to doDisplayAlert(argMesText) |
178 | #display alert |
179 | set strName to (name of current application) as text |
180 | if strName is "osascript" then |
181 | tell application "SystemUIServer" to activate |
182 | else |
183 | tell current application to activate |
184 | end if |
185 | # set strMes to "エラーしました" as text |
186 | set strMes to argMesText as text |
187 | try |
188 | tell application "SystemUIServer" |
189 | # tell application id "com.apple.Safari" |
190 | activate |
191 | set recordResponse to (display alert strMes buttons {"OK"} default button "OK" as critical giving up after 3) as record |
192 | end tell |
193 | on error |
194 | tell application "SystemUIServer" to quit |
195 | log "エラーしました" |
196 | return false |
197 | end try |
198 | return false |
199 | end doDisplayAlert |
AppleScriptで生成しました |