Clipboard to TextEdit - change bullet-list to 123.. - set to Clipboard

I have no idea how to go about doing this. I was hoping I could manage to do something with Automator or Shortcuts, but I can’t find a way.

I want to

1/ Select/Copy some text in Safari (produced by Filemaker’s Design-Report)
2/ Open this text in TextEdit
3/ Change the bullet-list from • to 123…
4/ Either set it to the clipboard, or open a new Stickies Note with the result.

Perhaps this can even be done without TextEdit??

(Ideally, I’d then like to make it into a Service or even a Shortcut, which can be run by either as a keyboard command or from a Menu).

Can anyone give me some pointers on how to do this?

You should post a sample of the data before and after conversion so we can test.

And you’re correct, you don’t need TextEdit. It can be done directly in Applescript

Ahh… good point.

Very simple. In TextEdit, Format/List… you can change any Bullet-List’s presentation. So, the top is what I get from Filemaker’s DDR, opened in Safari. The bottom, with numbers, is what matches Filemaker’s Script Editor.

•	If [ Get(ScriptParameter)="SmartGroup-PopOver" ]
•	Go to Object [ Object Name: "SmartGroup-PopOver" ]
•	Exit Script [ ]
•	End If

1	If [ Get(ScriptParameter)="SmartGroup-PopOver" ]
2	Go to Object [ Object Name: "SmartGroup-PopOver" ]
3	Exit Script [ ]
4	End If

(FWIW … The reason for wanting this, is that Filemaker’s Script Editor cannot be Searched. Also… I’m rediscovering the surprising usefulness of Stickies again, after many years of abandonment, especially when it’s married with apps assigned to multiple Spaces/Desktops (as I now do) and discovering that individual Stickies can be Space/Desktop specific too! That was a nice surprise. So together I think an Applescript, or other automation, could considerably improve the awkwardness of searching Filemaker scripts.)

I’ve included my suggestion below. A few comments:

  • This script worked with the example text in post 3.
  • The modified text is copied to the clipboard.
  • This script assumes that the text selected in Safari has already been copied to the clipboard–otherwise this can be done with GUI scripting or Automator.
  • Unexpected characters in the selected text may cause the script not to work as expected.
set theCharacter to "•"
set TID to text item delimiters
set theParagraphs to paragraphs of (the clipboard as «class ut16») -- the bulleted list is already on the clipboard

set paragraphCounter to 1
ignoring white space
	repeat with aParagraph in theParagraphs
		if aParagraph begins with theCharacter then
			set text item delimiters to theCharacter
			set theTextItems to text items of aParagraph
			set text item delimiters to (paragraphCounter as text)
			set contents of aParagraph to (theTextItems as text)
			set paragraphCounter to paragraphCounter + 1
		end if
	end repeat
end ignoring

set text item delimiters to linefeed
set the clipboard to (theParagraphs as text)
set text item delimiters to TID

the clipboard -- just to check the script works

Step 1 is problematic, and we’d need more information.

If a user selects the text that’s one thing, but if you want the script to set the text that is something else. Since Safari doesn’t have a selection property, you may need GUI scripting to copy the selected text.

If you are going to programatically choose the text based on it’s contents, you can get the entire chunk of text on a page and work with that.

Step 2 can be skipped.

Set 3 I would use text item delimters. (see below)

Step 4 is easy. (see below for clipboard, or we can do the note thing)

I have similar scripts that run from the script menu, and a couple services. Don’t know about shortcuts.

If you post a link to a page with the text you want to work with and explain how the text gets selected we can provide more detailed help.

Is this what you have in mind?



set textWithBullets to "
text before the bullets
more text before the bullets
 • First Bullet
 • Second bullet
 • third
 • fourth
 • fifth
 • sixth
unbulleted line
• incorecctly formatted bullet
 • 
• incorecctly formatted bullet
 • 
 unbulleted line
 • Ninth bullet
text after the bulleted text
more text after the bulleted text
"
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return & " • ", linefeed & " • "}
set textWithBullets to text items of textWithBullets

set newText to {item 1 of textWithBullets}
set textWithBullets to the rest of textWithBullets
repeat with x from 1 to count of textWithBullets
	set the end of newText to (" " & (x as text) & ". ") & item x of textWithBullets as text
end repeat
set AppleScript's text item delimiters to {return}
set newText to newText as text
set AppleScript's text item delimiters to saveTID
set the clipboard to newText
return newText

Wow! Thank you so much for taking the time to look at this.

Peavine

It worked for me too, however, as you mentioned… ‘unexpected characters’ may break it. So I started working through my longest script to see what might be the problem and after a bit realised that if I paste into TextEdit first then copy from there and run your script, it works perfectly, even with all the emojis I use in Comments! :slight_smile:

TextEdit is RTF, so I’m guessing that HTML to RTF first would be a ‘cure-all’ ?

estockly:

Thanks for the reply.

Ahh… yes it would only ever be manually selected. Out of curiosity, I just made a PDF of my DDR (Database Design Report) and it’s 580 pages of small type… and I expect my file is fairly tiny compared to ‘pros’.

What I’m doing is opening the DDR in Safari and searching for things that I’m working on in a script. But if the script is long, it can take ages to actually find them back in Filemaker because I can’t see what line they are on. So what I’m after, is to manually select the script in the DDR in Safari, then change the bullet-points to a numbered list, which I found worked and is easy in TextEdit and it matches perfectly with the scripts in Filemaker. So it really helps and speeds things up.

Here is a screen-grab of how I would select the script-code in Safari:

Grant. My statement regarding unexpected characters is not entirely correct–it should say that unexpected characters other than normal whitespace at the beginning of the line before the bullet character will cause the script not to work as expected. Anyways, it sounds like TextEdit works better for you which is great.

Here is a modified version of the above script which will only replace the first version of the “•” character.

set theCharacter to "•"
set TID to text item delimiters
set theParagraphs to paragraphs of (the clipboard) -- the bulleted list is already on the clipboard

set paragraphCounter to 1
repeat with aParagraph in theParagraphs
	if aParagraph contains theCharacter then
		set text item delimiters to theCharacter
		set theTextItems to text items of aParagraph
		set theTextItems to {item 1 of theTextItems} & {(items 2 thru -1 of theTextItems) as text}
		set text item delimiters to (paragraphCounter as text)
		set contents of aParagraph to (theTextItems as text)
		set paragraphCounter to paragraphCounter + 1
	end if
end repeat

set text item delimiters to linefeed
set the clipboard to (theParagraphs as text)
set text item delimiters to TID

the clipboard -- just for testing

or a slightly simplified version

set theCharacter to "•"
set TID to text item delimiters
set theParagraphs to paragraphs of (the clipboard) -- the bulleted list is already on the clipboard

set paragraphCounter to 1
set text item delimiters to theCharacter
repeat with aParagraph in theParagraphs
	if aParagraph contains theCharacter then
		set theTextItems to text items of aParagraph
		set contents of aParagraph to (item 1 of theTextItems) & (paragraphCounter as text) & ((items 2 thru -1 of theTextItems) as text)
		set paragraphCounter to paragraphCounter + 1
	end if
end repeat
set text item delimiters to linefeed
set the clipboard to (theParagraphs as text)
set text item delimiters to TID
the clipboard -- just for testing

I don’t know what I did, when I thought that the script was working, but I can’t get it to work again.

It does work, if I first paste into TextEdit, then copy, and then run the script. But otherwise no. What happens, is the bullet-points are stripped, but not replaced.

I’ve uploaded an example Filemaker DDR file (from one of Filemaker’s templates files) if anyone wants to take a look.

It’s a tiny file.

Open Summary.html, then you need to click on the Scripts link:

then select any of the bullet-list items:

_

Grant. I downloaded and looked at the DDR file as you suggest, and the “Script Steps” bulleted list does not contain any bullet characters when copied to the clipboard. As a result, none of the scripts in this thread will work. You could instead simply number the paragraphs of the clipboard text:

set theParagraphs to paragraphs of (the clipboard)

set paragraphCounter to 1
repeat with aParagraph in theParagraphs
	set contents of aParagraph to (paragraphCounter as text) & space & aParagraph
	set paragraphCounter to paragraphCounter + 1
end repeat

set TID to text item delimiters
set text item delimiters to linefeed
set theParagraphs to theParagraphs as text
set text item delimiters to TID
set the clipboard to theParagraphs
theParagraphs -- just to check

The HTML vs RTF issue is likely that the former copy consists of html code rather than rendered text. In this case, that means an html list tag (

  • ) rather than a bullet character. The browser of course, renders the list tag as a bullet. Here is the relevant extract from the clipboard’s contents (as public.html… you can see for yourself using Clipboard Viewer) when the first two lines are selected in the first record from your upload:

    <li>Allow User Abort [ Off ]</li><li>If [ Get ( SystemPlatform ) = 3 or Get ( SystemPlatform ) = 4 ]</li>
    

    And apparently, when you paste it into textedit, the list tags are converted into bullets automatically.

    Perhaps the search/replace could be modified to include the list tag.

  • By the way, if you get the clipboard as UTF-16 then you get something that might be easier to work with.

    By the way, each line begins with <bullet U+2022> characters.

    
    the clipboard as «class ut16»
    
    	•	Allow User Abort [ Off ]
    	•	If [ Get ( SystemPlatform ) = 3 or Get ( SystemPlatform ) = 4 ]
    

    Thanks Mockman. I’ve modified my script in post 4, which now works correctly with the “Script Steps” bulleted list. It returns the same result as my script in post 11, so either could be used.

    Thank you so much guys. It’s now working perfectly.

    I’ve been playing around trying to take it a bit further so I can run it as a Service with: copy the selection to clipboard, convert to 123…, open Stickies, new note, paste. However, I’ve stumbled across the modern MacOS security issues and limitations on keyboard control etc. So I’m wondering what is ‘best practice’ for doing this now? Do you use keyboard control and add a Security exception, or is there AppleScript code that does it differently? I guess some sort of ‘set selection to clipboard’ exists, but Stickies doesn’t have any AppleScript support, AFAIK, so how is this best handled?

    _
    There is one aspect that I’ve noticed when I have long code/comments, which wrap, depending on window width, which is that the html conversion is altering the formatting, so the list is no longer a list and therefore, the formatting/spacing to the left makes it harder to read, e.g:



    Anything that could be done here?

    Just noticed something else…

    Copy in Safari and open the Clipboard via Finder/Edit/Show Clipboard and it turns out, the OS is already converting to RTF.

    Grant. As you are aware, there is not a best method for running scripts, but Automator seems a good choice given your needs. There’s a lot of information on this online, and the following is one example:

    https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/MakeaSystem-WideService.html

    Since you have to make a manual selection in Safari, it seems almost no effort to manually copy that selection to the clipboard. You could also use the Automator option entitled “Workflow receives current text”, and GUI scripting is another alternative.

    As regards the Stickies app, GUI scripting can be used to accomplish what you want. For example,

    tell application "Stickies" to activate
    delay 0.2 -- test different values
    tell application "System Events" to tell process "Stickies"
    	click menu item "New Note" of menu "File" of menu bar 1
    	delay 0.2 -- test different values
    	click menu item "Paste" of menu "Edit" of menu bar 1
    end tell
    

    As a general rule, the clipboard contains multiple versions of whatever you copy. I guess this is necessary so the paste will be relevant to whatever application or context you’re pasting into. When you use ‘the clipboard’ command along with the ‘as’ option, the system will try to pick something that is closest to what it deems you to be requesting.

    When you run ‘clipboard info’, you can see what types of data are available. In this case, it revealed the different types, including the UTF-16 data, which seemed to best represent the desired output. Here is the result with the last two bullet points selected:

    [format]{{«class weba», 905}, {«class RTF », 860}, {«class HTML», 457}, {«class utf8», 16}, {«class ut16», 46}, {uniform styles, 476}, {string, 22}, {scrap styles, 82}, {Unicode text, 32}, {uniform styles, 476}, {scrap styles, 82}}[/format]

    With this, you can run ‘the clipboard as «class ut16»’, as well as several others.

    Finally, your mac should have an app called Clipboard Viewer which allows you to see the contents. It’s not as visually decent as what the ‘show clipboard’ menu item shows but unlike that, it shows the many variations (albeit not always comprehensibly).

    Thank you for the further help, Peavine and Mockman. That gives me some useful pointers to work with, particularly the UI scripting, which should let me do the next step. And thanks also to robertfern and estockly for your help too. :slight_smile: