AppleScript to take text and turn it into pasteable HTML - macos

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 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:
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.
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.
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:
Bug number 777 link
Change <bugzilla_server> to your working URL.
set bug_number to the clipboard
set the_text to "Bug number " & bug_number & " link"
set the clipboard to the_text

Related

Automator adding unwanted line breaks

I'm using Automator to create an HTML page and everything works great but I'm running into one small problem. The user is asked for information at the beginning that is then set into variables. The page is created by grabbing some code using Get Specified Text and copying it to the clipboard, getting one of the variables and then putting them both into a text document. This process is then repeated several times, eventually creating an HTML file. I'm running into issues because Automator is creating line breaks (maybe carriage returns?) in between each bit of specified text and each variable. So, what I want to look like this:
<code grabbed using "Get Specified Text" followed by a Variable. And now some more text and another Variable.>
ends up looking like this:
<code grabbed using "Get Specified Text" followed by a
Variable
. And now some more text and another
Variable
.>
This is breaking my page in a few parts. Is there a way to prevent these line breaks?
The items passed along from action to action are in a list, so it appears that setting the TextEdit contents separates the individual items by a newline, which is the normal paragraph delimiter.
Many of the text actions assume TextEdit and/or rich text and don’t use variables (or get along with other plain text actions), so a Run AppleScript action can be used before an action to convert or concatenate items, for example (Mojave):
Automator (or TextEdit for that matter) isn’t really a very good tool for HTML editing. You might take a look at BBEdit (the light version is free), which also has excellent AppleScript support.
EDIT:
Use the following in a Run AppleScript action to combine the text using a specified delimiter (this example uses an empty string):
on run {input, parameters}
set separator to "" -- text to separate the items with
set tempTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to separator
set output to input as text
set AppleScript's text item delimiters to tempTID
return output
end run

Extract Hyperlinks From Rich Text Clipboard Contents Or Text Selection On The Mac

I would like to be able to get a list of all the hyperlinked URLs in any formatted text that I select on the Mac (formatted text such as a web page or a word processor document).
Preferably I'd like use Applescript or Automator to extract this list of hyperlinks from the text (so that I can then use Applescript to perform further processing on these URLs).
Note that I am talking about hyperlinks being extracted from formatted text, not just extracting URLs from text containing plaintext URLs.
This hyperlink extraction from formatted text seems like it should be a simple programming task, but I have been struggling to find a way to do this in either Applescript or Automator.
Automator can be set to accept rich text input from a text selection, or can input rich text from the clipboard, but I cannot find any way to access this rich text as a string within Automator or Applescript, such that I can then extract the hyperlinked URLs from the string of rich text data.
Once I get access to the rich text data as a string, there will be no problem in extracting the URLs.
Any suggestions on how I might solve this issue are gratefully received.
Applescript itself does not unpack embedded text, so you'll have to use a helper app one way or another. You can use do shell script 'textutil' to do some unimbedding of links:
perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' |
textutil -stdin -stdout -convert html -format rtf
Then, you'll have to extract the URLs. I would suggest using the Automator action 'Extract Data' to do this. If you set the whole thing up as an Automator Workflow, you could invoke it from Applescript. Or, if you save it as a Service, you can just run the whole thing from the Service.
Here's a screenshot of that method that should show what you need:
Let me know if you have questions. You can see variations on this technique here.
Update:
If you want to create this into a Service, it is difficult to coerce the built-in input from Automator into RTF. An effective method is to ignore the input and do a
keystroke "c" using command down
to copy the selected text to the clipboard and then use the workflow from there. See example:

How to disable formatting in TextEdit on Mac Yosemite

When I copy something and paste it from buffer in TextEdit, it saves text formatting (font-color, background-color, font-style) How can I turn that off?
I don't think you will be able to turn it off. But you can tweak it in the copy/paste process.
Take a look at this article here as it explains how you can copy/paste using a certain combination of keys to skip formatting.
Paste the copied text and match current style by using Command+Option+Shift+V.
Notice the difference from the normal Command+V paste trick, which would include the formatting.
If you don't want to save text with formatting, you should work with plain text format: menu Format > Make Plain Text (⇧⌘T). You can also set plain text format as your default document format in TextEdit Preferences.

Remove spaces from a string of text in clipboard

This is maybe a weird request but hear me out:
I have a huge database at my shop containing product codes, like 87 445 G 6 which I need to check for availability on a supplier's website. The problem is, the supplier's website consists of a web form in which I have to enter the code without spaces, so imagine that I have to manually delete spaces every time I paste a code or write it manually without.
I can't edit the database from which I copy the codes.
I wonder if some sort of plugin, script, or trick can be used directly in browser on the supplier's web form, or some software to modify how the windows clipboard works, maybe some option to copy text without spaces. Using Windows XP.
The OP has probably moved on, but for anyone else looking here, my approach was to tackle this from the windows clipboard side.
For background: I keep a list of my credit card info in Keepass. Sometimes (poorly coded) shopping cart checkout forms don't like spaces in between card numbers. I like storing them with spaces since it's easier to read off that way.
There's various Windows clipboard utilites out there, but it took me a while to find one that could do some processing on the clipboard contents and pasting it out - Clipboard Help and Spell
The program has a way to "save" a bunch of text transformations, and even assign the action to a hotkey.
For reference, my "Find and Replace" action is to find "\s" (without quotes) and leave the Replace textbox empty. "\s" will match whitespace character.
Use the javascript console
You could use the javascript console for your browser to edit the textarea after you paste.
Using Google Chrome (or Firefox)
Paste your text in the text area.
Right click the text area and click Inspect Element
Look at the id for the element
Now switch to the console view
then run these lines (making sure to replace with 'the-id' with your id)
var my_text_area = document.getElementById('the-id'); // Put your id in here
my_text_area.value = my_text_area.value.replace(/ /g,"") // Deletes just spaces
It's even simpler if you have access to jQuery:
$('#the-id').val($('#the-id').val().replace(/ /g, ""))
The replace function is simply using regular expressions to convert spaces to nothing. If you want to replace all whitespace (including newlines) you would use .replace(/\s/g,"").
For firefox, the names are the same but the UI is a little bit different.
Use greasemonkey
You can either write a greasemonkey plugin or try to find one that fits your needs.

Capture user input by opening a text editor with content

From a bash script, I'd like to
Open the default text editor for current user
Paste a string $original_content in it
Once the user modifies the content then closes the text editor,
Capture the modified string into a variable $modified_content
Then save $modified_content to an $output_file
Google searches for capturing user input shows read which is not what I'm looking for.
Can someone point me to the right direction?
Thank you
This method should hopefully work for most editors:
#!/bin/bash
original_content="Your original content"
echo $original_content > /tmp/user_input.tmp
# For example:
# DEFAULT_EDITOR=/usr/bin/vi
$DEFAULT_EDITOR /tmp/user_input.tmp
modified_content=`cat /tmp/user_input.tmp`
echo $modified_content > /tmp/output_file
This script may be a little drawn out but it performs all the actions you wanted except for the pasting part, since you'd probably have to accommodate for all varieties of editors to properly "paste" a string. This script utilizes the benefit that calling most editors with a filename as a parameter opens that file for editing thereby "pasting" your $original_content in the editor.

Resources