2017-08-29 1 views
1

Débutant complet ici alors s'il vous plaît laissez-moi savoir si je dois clarifier ou autrement améliorer ma question. J'ai effectué plusieurs recherches à l'aide de différents mots clés et je n'ai pas trouvé de solution à mon problème. Je veux créer un script (Applescript) qui, une fois déclenché, me permettra de coller un clip de texte web ajouté avec l'attribution de la source et un horodatage, sans perdre aucun lien possible dans le texte sélectionné.Puis-je créer un Applescript qui va coller un clip de texte Web ajouté avec l'attribution de la source et un horodatage, tout en conservant les liens incorporés?

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

Ne sachant pas quoi que ce soit la programmation sage, j'ai pu concocter le script suivant (AppleScript) après quelques jours de recherche sur le Web.

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

J'ai été forcé dans cette solution de contournement parce que quand je la commande « set » les liens intégrés se sont effacés du texte Web sélectionné.

Bien que ce script ne fonctionne, il est assez lourd et lent. J'ai essayé toutes sortes de choses différentes (y compris des commandes de script shell) mais jusqu'ici rien d'autre n'a fonctionné.

Quelqu'un peut me aider à créer un script plus élégant et plus rapide qui maintient encore les liens intégrés dans le texte Web sélectionné?

Merci,

Je cours MacOS Sierra (Version: 10.12.6)

+0

Votre approche est pas mal, en général, bien que beaucoup des besoins de script être nettoyé. Vous devez utiliser le presse-papiers pour transférer des liens de liaison de données formatés. Question: Quel est le résultat final souhaité? Est-ce qu'un lien et des données source sont assis sur le presse-papiers prêt à coller quelque part? Parce que vous le supprimez de votre document TextEdit? – jweaks

+0

@jweaks Merci pour les commentaires. Mon résultat final souhaité est d'avoir mon clip de texte Web sélectionné, ses informations de source et un emplacement d'horodatage dans le presse-papiers prêt à coller comme une entrée unique dans mon application de prise de notes préférée. –

+0

L'approche la plus propre et la plus rapide serait de coller le script dans l'application de prise de notes, de sorte que vous puissiez éviter d'utiliser TextEdit. Quelle est l'application? Est-ce scriptable? – jweaks

Répondre

0

Voici le script (un Cocoa-AppleScript ) pour copier la sélection dans le presse-papiers, et d'ajouter un texte aux données HTML du presse-papiers.

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 

Si vous n'avez pas besoin d'obtenir des images de la sélection, supprimer ces lignes dans le script:

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 Merci. Votre script (bien au-dessus de mon niveau) permet un flux de travail simple et répond entièrement à mes exigences. –

+0

@jweaks jweaks Merci pour votre aide continue. La contribution de jackjr300 a résolu mon problème. –