开发者

AppleScript to take text and turn it into pasteable HTML

开发者 https://www.devze.com 2022-12-13 12:06 出处:网络
We work with bugzilla.Whenever you need to query a ticket you just need to know the bugid (integer) and you simply prepend this to it.

We work with bugzilla. Whenever you need to query a ticket you just need to know the bugid (integer) and you simply prepend this to it.

http://<bugzilla_server>/bugzilla/show_bug.cgi?id=<bug_id>

Suppose I have a bug link which looks like this 777. If I select and copy this it is preserved on the pasteboard so when I paste this开发者_JAVA百科 into mail it will correctly preserve the link and it's attributes.

What I am looking for is to simple type '777' select it and run an applescript on it and replace it with a link like the one above. Can anyone help me out??


The following AppleScript will take the contents of the clipboard and replace it with the URL prepended:

set the clipboard to "http://bugzilla_server/bugzilla/show_bug.cgi?id=" & (the clipboard)

You can compile that to an AppleScript scpt and make it available in a Scripts folder or compile it to a launchable app:

osacompile -e 'set the clipboard to "http://bugzilla_server/bugzilla/show_bug.cgi?id=" & (the clipboard)' -o replacebug.scpt  # or -o replacebug.app

If your primary use case for this is in composing mail in Mail.app, this may not be the most user-friendly approach, though. If you are using Snow Leopard (10.6), a simpler solution is to take advantage of the new Text Substitution feature. Open the System Preferences -> Language & Text preference panel, select the Text tab, and click + to add a new substitution, perhaps:

Replace   With

  (b)     http://bugzilla_server/bugzilla/show_bug.cgi?id=

Then, in Mail.app, start a New Message and, with the cursor clicked within the text body, do a Control click of the mouse to bring up the contextual menu. From it, select Substitutions -> Text Replacement. From now on, as you are typing in the text body of the email when you type:

(b)777

the (b) will automatically change to the URL text you saved:

http://bugzilla_server/bugzilla/show_bug.cgi?id=777

This will also work in other Cocoa text-enabled applications like Safari.

EDIT:

When talking about composing URL links in email, there are at least three different formats of email, each with a different solution. Since you don't say which kind you are using, I'll cover all three:

  1. Plain text format - There's no way to "hide" the URL in the composed email although some email readers might present a clickable link for a plain-text URL.

  2. HTML-formatted email - Apple's Mail.app does not support composing email in this format although it will display it. Using some other mail writer client or your own program, it's easy enough to compose a link using a standard HTML anchor <a href=...> tag.

  3. Rich Text Format email - AFAIK, this is the only way to compose a URL link with Mail.app. Unfortunately, there does not appear to be an easy way to directly create an RTF hyperlink using AppleScript commands. Based on a suggestion here, this is a way to do it by creating a modifiable RTF template via the clipboard.

    • In TextEdit.app, create a new Document window.
    • Insert the text you want to appear in the email, i.e. 777.
    • Select the text (⌘A) then add a link (⌘K). Enter the full URL also with 777 into the "Link destination" field; click OK.
    • Modify the text format as desired with Format menu commands.
    • Save the file (⇧⌘S) as temp.rtf with File Format -> Rich Text Format.
    • Close the document window.
    • Open a document window (⌘O) selecting file temp.rtf and selecting Ignore rich text commands.
    • Insert the following before the first line in the file:

      #!/bin/sh
      sed -e "s/777/$(pbpaste -Prefer txt)/g" <<EOF | pbcopy -Prefer rtf
      
    • Append EOF as a separate line at the end of the file.

      It should now look something like this:

      #!/bin/sh
      sed -e "s/777/$(pbpaste -Prefer txt)/g" <<EOF | pbcopy -Prefer rtf
      {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250
      {\fonttbl\f0\fswiss\fcharset0 Helvetica;}
      {\colortbl;\red255\green255\blue255;}
      \margl1440\margr1440\vieww9000\viewh8400\viewkind0
      \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
      {\field{\*\fldinst{HYPERLINK "http://bugzilla_server/bugzilla/show_bug.cgi?id=777"}}{\fldrslt 
      \f0\fs24 \cf0 777}}}
      EOF
      
    • Save this as a Plain Text file and execute directly as a shell script or call it via the AppleScript do shell script command.

    This kind of solution will work with most other applications that support Rich Text format.


Not sure exactly the function you're looking for, but this will take a number from your clipboard and process it into a link and put the link on the clipboard as a standard href URL that will work in plain or rich text, like: <a href="http://<bugzilla_server>/bugzilla/show_bug.cgi?id=777" title="777">Bug number 777 link</a>

Change <bugzilla_server> to your working URL.

set bug_number to the clipboard

set the_text to "<a href=\"http://<bugzilla_server>/bugzilla/show_bug.cgi?id=" & bug_number & "\" title=\"" & bug_number & "\">Bug number " & bug_number & " link</a>"

set the clipboard to the_text
0

精彩评论

暂无评论...
验证码 换一张
取 消