我可以创建一个 Applescript 来粘贴带有源属性和时间戳的 Web 文本剪辑,同时保留嵌入式链接吗?
Can I create an Applescript that will paste a web text clip appended with source attribution and a timestamp, while maintaining embedded links?
这里是新手,如果我需要澄清或改进我的问题,请告诉我。
我使用不同的关键字进行了多次搜索,但未能找到解决我的问题的方法。
我想创建一个脚本 (Applescript),当它被触发时,我可以粘贴一个带有源属性和时间戳的网络文本剪辑,而不会丢失所选文本中任何可能的嵌入链接。
我不太了解任何编程方面的知识,经过几天的网络搜索,我能够拼凑出以下脚本 (Applescript)。
-- clear the clipboard
tell application "Finder"
set the clipboard to " "
delay 0.1
end tell
-- copy selected text
tell application "Safari"
activate
tell application "System Events"
tell process "Safari"
keystroke "c" using {command down}
delay 0.1
end tell
end tell
end tell
-- open and paste web clip into specified TextEdit file
tell application "TextEdit"
activate
open "Macintosh HD:Users:Web:Documents:Web Text Clips:Web_Text_Clips.rtf"
delay 0.2
tell application "System Events"
tell process "TextEdit"
keystroke "v" using {command down}
delay 0.1
end tell
end tell
end tell
-- get, format and copy source info and timestamp
tell application "Safari"
activate
set theLongDate to current date
set theWindowName to the name of the front window
set theURL to the URL of the front document
set writeString to "- - - - - " & return & "From: " & theURL & return & "Page Title: " & theWindowName & return & "Date: " & theLongDate
set the clipboard to writeString
end tell
-- paste source info and timestamp into predefined position of the specified TextEdit file
tell application "TextEdit"
activate
tell application "System Events"
tell process "TextEdit"
keystroke (ASCII character 31) using command down
keystroke return
keystroke return
keystroke "v" using {command down}
delay 0.1
end tell
end tell
end tell
-- copy content of specified TextEdit file
tell application "TextEdit"
activate
tell application "System Events"
tell process "TextEdit"
keystroke "a" using {command down}
keystroke "c" using {command down}
delay 0.1
end tell
end tell
end tell
-- delete content of specified TextEdit file
tell application "TextEdit"
activate
tell application "System Events"
tell process "TextEdit"
keystroke "a" using {command down}
keystroke "x" using {command down}
delay 0.1
end tell
end tell
end tell
-- save specified TextEdit file and quit TextEdit
tell application "TextEdit"
save "Macintosh HD:Users:Web:Documents:Web Text Clips:Web_Text_Clips.rtf"
quit
end tell
我被迫采用此解决方法,因为当我使用“设置”命令时,嵌入的链接已从选定的 Web 文本中删除。
虽然此脚本确实有效,但它非常麻烦且缓慢。我已经尝试了各种不同的东西(包括一些 shell 脚本命令)但到目前为止没有任何其他效果。
任何人都可以帮助我创建一个更优雅、更快速的脚本,该脚本仍然保留所选 Web 文本中的嵌入式链接吗?
谢谢,
我是运行 MacOS Sierra(版本:10.12.6)
这是脚本(Cocoa-AppleScript),用于将所选内容复制到剪贴板,并将一些文本附加到剪贴板的 HTML 数据.
use framework "Foundation"
use scripting additions
set beginningStr to return & return & "- - - - - " & return & "From: "
set theLongDate to current date
tell application "Safari"
if (do JavaScript "document.execCommand('copy')" in document 1) then -- copy the selection to the clipboard
set {theTitle, theURL} to {name, URL} of the front document
set writeString to beginningStr & theURL & return & "Page Title: " & theTitle & return & "Date: " & theLongDate
set r to my appendStringToClipboard(writeString, theURL, length of beginningStr)
if r is "" then
my goToNoteApp()
else
display alert r
end if
else
display alert "Error: can't copy to clipboard, check the selection"
end if
end tell
on goToNoteApp() --**** you must change the name of the application to the name of your note-taking app
activate application "Notes"
-- now you can paste
end goToNoteApp
on appendStringToClipboard(t, u, n) -- params: t = the text to append, u = the url, n = the number of characters in the text before the url
tell current application
set pboard to its NSPasteboard's generalPasteboard()
-- ** if the clipboard contains HTML data **
if (pboard's availableTypeFromArray:{its NSPasteboardTypeHTML}) is missing value then return "No HTML data in the clipboard"
set tData to (pboard's stringForType:(its NSPasteboardTypeHTML))'s dataUsingEncoding:(its NSUnicodeStringEncoding) -- get the HTML data
set attrString to its (NSMutableAttributedString's alloc()'s initWithHTML:tData documentAttributes:(missing value)) -- get an attributed string from the HTML data
if attrString is missing value then return "The HTML data can’t be decoded."
set t_RTFD to (pboard's availableTypeFromArray:{its NSRTFDPboardType}) -- if the clipboard contains an image
set myFont to its (NSFont's fontWithName:"Helvetica" |size|:14) -- *** the font and the size of the appended text (you can change it) ***
set myColor to its (NSColor's blackColor()) -- *** the color of the appended text (you can change it)***
set theDict to its (NSDictionary's alloc()'s initWithObjectsAndKeys_(myColor, its NSForegroundColorAttributeName, myFont, its NSFontAttributeName, missing value))
set stringToAppend to its ((NSMutableAttributedString's alloc)'s initWithString:t attributes:theDict) -- create an attributed string ( the text, url, title and the timestamp )
stringToAppend's addAttribute:(its NSLinkAttributeName) value:u range:{location:n, |length|:length of u} -- add attribute for the URL (make a clickable link)
attrString's appendAttributedString:stringToAppend --- append the text, the url, the title and the timestamp to the attrString (the contents of the selection)
pboard's declareTypes:{its NSPasteboardTypeRTF, its NSPasteboardTypeHTML} owner:me
-- *** the HTML data and the appended text ***
set {tData, err} to attrString's dataFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSHTML"} |error|:(reference)
pboard's setData:tData forType:(its NSPasteboardTypeHTML) ---- put the new HTML data into the clipboard
-- *** the RTF data and the appended text ***
set tData to attrString's RTFFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSRTF"}
pboard's setData:tData forType:(its NSPasteboardTypeRTF) ---- put the new RTF data into the clipboard
if t_RTFD is not missing value then -- the clipboard contains an image
-- *** the RTFD data and the appended text ***
pboard's addTypes:{its NSPasteboardTypeRTFD} owner:me -- add the RTFD type
set tData to attrString's RTFDFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSRTFD"}
pboard's setData:tData forType:(its NSPasteboardTypeRTFD) ---- put the new RTFD data into the clipboard
end if
return ""
end tell
end appendStringToClipboard
如果您不需要从选择中获取图像,请删除脚本中的这些行:
if t_RTFD is not missing value then -- the clipboard contains an image
-- *** the RTFD data and the appended text ***
pboard's addTypes:{its NSPasteboardTypeRTFD} owner:me -- add the RTFD type
set tData to attrString's RTFDFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSRTFD"}
pboard's setData:tData forType:(its NSPasteboardTypeRTFD) ---- put the new RTFD data into the clipboard
end if
这里是新手,如果我需要澄清或改进我的问题,请告诉我。 我使用不同的关键字进行了多次搜索,但未能找到解决我的问题的方法。
我想创建一个脚本 (Applescript),当它被触发时,我可以粘贴一个带有源属性和时间戳的网络文本剪辑,而不会丢失所选文本中任何可能的嵌入链接。
我不太了解任何编程方面的知识,经过几天的网络搜索,我能够拼凑出以下脚本 (Applescript)。
-- clear the clipboard
tell application "Finder"
set the clipboard to " "
delay 0.1
end tell
-- copy selected text
tell application "Safari"
activate
tell application "System Events"
tell process "Safari"
keystroke "c" using {command down}
delay 0.1
end tell
end tell
end tell
-- open and paste web clip into specified TextEdit file
tell application "TextEdit"
activate
open "Macintosh HD:Users:Web:Documents:Web Text Clips:Web_Text_Clips.rtf"
delay 0.2
tell application "System Events"
tell process "TextEdit"
keystroke "v" using {command down}
delay 0.1
end tell
end tell
end tell
-- get, format and copy source info and timestamp
tell application "Safari"
activate
set theLongDate to current date
set theWindowName to the name of the front window
set theURL to the URL of the front document
set writeString to "- - - - - " & return & "From: " & theURL & return & "Page Title: " & theWindowName & return & "Date: " & theLongDate
set the clipboard to writeString
end tell
-- paste source info and timestamp into predefined position of the specified TextEdit file
tell application "TextEdit"
activate
tell application "System Events"
tell process "TextEdit"
keystroke (ASCII character 31) using command down
keystroke return
keystroke return
keystroke "v" using {command down}
delay 0.1
end tell
end tell
end tell
-- copy content of specified TextEdit file
tell application "TextEdit"
activate
tell application "System Events"
tell process "TextEdit"
keystroke "a" using {command down}
keystroke "c" using {command down}
delay 0.1
end tell
end tell
end tell
-- delete content of specified TextEdit file
tell application "TextEdit"
activate
tell application "System Events"
tell process "TextEdit"
keystroke "a" using {command down}
keystroke "x" using {command down}
delay 0.1
end tell
end tell
end tell
-- save specified TextEdit file and quit TextEdit
tell application "TextEdit"
save "Macintosh HD:Users:Web:Documents:Web Text Clips:Web_Text_Clips.rtf"
quit
end tell
我被迫采用此解决方法,因为当我使用“设置”命令时,嵌入的链接已从选定的 Web 文本中删除。
虽然此脚本确实有效,但它非常麻烦且缓慢。我已经尝试了各种不同的东西(包括一些 shell 脚本命令)但到目前为止没有任何其他效果。
任何人都可以帮助我创建一个更优雅、更快速的脚本,该脚本仍然保留所选 Web 文本中的嵌入式链接吗?
谢谢,
我是运行 MacOS Sierra(版本:10.12.6)
这是脚本(Cocoa-AppleScript),用于将所选内容复制到剪贴板,并将一些文本附加到剪贴板的 HTML 数据.
use framework "Foundation"
use scripting additions
set beginningStr to return & return & "- - - - - " & return & "From: "
set theLongDate to current date
tell application "Safari"
if (do JavaScript "document.execCommand('copy')" in document 1) then -- copy the selection to the clipboard
set {theTitle, theURL} to {name, URL} of the front document
set writeString to beginningStr & theURL & return & "Page Title: " & theTitle & return & "Date: " & theLongDate
set r to my appendStringToClipboard(writeString, theURL, length of beginningStr)
if r is "" then
my goToNoteApp()
else
display alert r
end if
else
display alert "Error: can't copy to clipboard, check the selection"
end if
end tell
on goToNoteApp() --**** you must change the name of the application to the name of your note-taking app
activate application "Notes"
-- now you can paste
end goToNoteApp
on appendStringToClipboard(t, u, n) -- params: t = the text to append, u = the url, n = the number of characters in the text before the url
tell current application
set pboard to its NSPasteboard's generalPasteboard()
-- ** if the clipboard contains HTML data **
if (pboard's availableTypeFromArray:{its NSPasteboardTypeHTML}) is missing value then return "No HTML data in the clipboard"
set tData to (pboard's stringForType:(its NSPasteboardTypeHTML))'s dataUsingEncoding:(its NSUnicodeStringEncoding) -- get the HTML data
set attrString to its (NSMutableAttributedString's alloc()'s initWithHTML:tData documentAttributes:(missing value)) -- get an attributed string from the HTML data
if attrString is missing value then return "The HTML data can’t be decoded."
set t_RTFD to (pboard's availableTypeFromArray:{its NSRTFDPboardType}) -- if the clipboard contains an image
set myFont to its (NSFont's fontWithName:"Helvetica" |size|:14) -- *** the font and the size of the appended text (you can change it) ***
set myColor to its (NSColor's blackColor()) -- *** the color of the appended text (you can change it)***
set theDict to its (NSDictionary's alloc()'s initWithObjectsAndKeys_(myColor, its NSForegroundColorAttributeName, myFont, its NSFontAttributeName, missing value))
set stringToAppend to its ((NSMutableAttributedString's alloc)'s initWithString:t attributes:theDict) -- create an attributed string ( the text, url, title and the timestamp )
stringToAppend's addAttribute:(its NSLinkAttributeName) value:u range:{location:n, |length|:length of u} -- add attribute for the URL (make a clickable link)
attrString's appendAttributedString:stringToAppend --- append the text, the url, the title and the timestamp to the attrString (the contents of the selection)
pboard's declareTypes:{its NSPasteboardTypeRTF, its NSPasteboardTypeHTML} owner:me
-- *** the HTML data and the appended text ***
set {tData, err} to attrString's dataFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSHTML"} |error|:(reference)
pboard's setData:tData forType:(its NSPasteboardTypeHTML) ---- put the new HTML data into the clipboard
-- *** the RTF data and the appended text ***
set tData to attrString's RTFFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSRTF"}
pboard's setData:tData forType:(its NSPasteboardTypeRTF) ---- put the new RTF data into the clipboard
if t_RTFD is not missing value then -- the clipboard contains an image
-- *** the RTFD data and the appended text ***
pboard's addTypes:{its NSPasteboardTypeRTFD} owner:me -- add the RTFD type
set tData to attrString's RTFDFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSRTFD"}
pboard's setData:tData forType:(its NSPasteboardTypeRTFD) ---- put the new RTFD data into the clipboard
end if
return ""
end tell
end appendStringToClipboard
如果您不需要从选择中获取图像,请删除脚本中的这些行:
if t_RTFD is not missing value then -- the clipboard contains an image
-- *** the RTFD data and the appended text ***
pboard's addTypes:{its NSPasteboardTypeRTFD} owner:me -- add the RTFD type
set tData to attrString's RTFDFromRange:{location:0, |length|:attrString's |length|()} documentAttributes:{DocumentType:"NSRTFD"}
pboard's setData:tData forType:(its NSPasteboardTypeRTFD) ---- put the new RTFD data into the clipboard
end if