2017-08-29 1 views
1

Komplette Neuling hier, also lass es mich wissen, wenn ich meine Frage klären oder anderweitig verbessern muss. Ich habe mehrere Male mit verschiedenen Keywords gesucht und konnte keine Lösung für mein Problem finden.Kann ich ein Applescript erstellen, das einen Web-Textclip mit Quellenattribut und einen Zeitstempel einfügt, während eingebettete Links beibehalten werden?

Ich möchte ein Skript (Applescript) erstellen, das es mir ermöglicht, einen Web-Textclip mit Quellenattribut und einem Zeitstempel einzufügen, ohne dabei mögliche eingebettete Links im ausgewählten Text zu verlieren.

Here is a screen shot of what I want to achieve:

Nicht so gut wie nichts wissen, weise Programmierung, konnte ich das folgende Skript (Applescript) nach ein paar Tagen von Web-Suche, um zusammenschustern.

-- 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 

Ich wurde in diese Problemumgehung gezwungen, denn wenn ich die „set“ die eingebetteten Links von dem ausgewählten Web-Text ausgelöscht wurden Befehl.

Während dieses Skript funktioniert, ist es ziemlich umständlich und langsam. Ich habe alle möglichen Dinge ausprobiert (einschließlich einiger Shell-Skriptbefehle), aber bis jetzt hat nichts anderes funktioniert.

Kann mir jemand bei der Erstellung eines eleganteren und schnelleren Skripts helfen, das die eingebetteten Links im ausgewählten Webtext noch enthält?

Danke,

ich MacOS Sierra leite (Version: 10.12.6)

+0

Ihr Ansatz ist nicht schlecht, im Allgemeinen, obwohl viele der Skript Bedürfnisse aufgeräumt werden. Sie müssen die Zwischenablage verwenden, um formatierte Datenlink-Links zu übertragen. Frage: Was ist Ihr gewünschtes Endergebnis? Sind Link- und Quelldaten in der Zwischenablage, die irgendwo eingefügt werden können? Weil Sie es aus Ihrem TextEdit-Dokument löschen? – jweaks

+0

@jweaks Danke für die Rückmeldung. Mein gewünschtes Endergebnis besteht darin, meinen ausgewählten Webtextclip, seine Quellinformationen und einen Zeitstempel in der Zwischenablage anzuordnen, um ihn als einen einzigen Eintrag in meine bevorzugte Notizenanwendung einzufügen. –

+0

Der sauberste und schnellste Weg wäre, das Skript in die Notizen-App einzufügen, so dass Sie TextEdit vermeiden können. Was ist die App? Ist es skriptfähig? – jweaks

Antwort

0

Hier ist das Skript (a Cocoa-Apple), um die Auswahl in die Zwischenablage zu kopieren, und einen Text anfügen zu den HTML-Daten der Zwischenablage.

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 

Wenn Sie nicht brauchen, Bilder aus der Auswahl zu erhalten, diese Zeilen in dem Skript entfernen:

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 
+0

jackjr300 Danke. Ihr (weit über mein Level) Skript ermöglicht einen einfachen Workflow und entspricht voll meinen Anforderungen. –

+0

@jweaks jweaks Danke für Ihre weitere Hilfe. jackjr300 Beitrag hat mein Problem gelöst. –

Verwandte Themen