Search and Replace within the first xx characters of Clipboard?

I'd like to search and replace certain text within the first xx number of characters within the clipboard.
I've tried using a substring to no avail.

ie. Search and replace the #(sign) with "testtext" only within the first 10 characters of the clipboard.

1 Like

Can you post your method here, even if it didn't work ?

I really didn't really get anywhere at all, only to know that I can 'get' the first 10 characters of the clipboard. I could save that to a variable then search and replace in that variable, but then how to get that result to replace the first 10 characters of the clipboard.

This seems to work, I'll be testing it later- busy Saturday!!!
If someone has a more 'proper' way, if there is one =) please let me know, thank you.

Search and Replace first 10 xx characters of clipboard.kmmacros (3.5 KB)

Mine is not too dissimilar to yours. I don't know what you mean by "proper", but see how they compare with each other and provide any feedback you think would improve things.

In my example, I've taken the text from your original post, and replaced all occurrences of the letter e with an uppercase E, limiting the search/replace scope to the first 30 characters of the clipboard text.

When you run the macro, you'll see the result.

image Search and Replace Within Substring of Clipboard.kmmacros (46.1 KB)

1 Like

Nice, thanx, I know there is no 'proper' , just sort of kidding around.
Yeah, pretty similar, really appreciate you responding, it got me looking deeper and I found the 'length' function which I'm sure will come in handy down the road for something else. =)

You could do it using a scripting method. Here's an AppleScript that does it (the result can be seen in red at the bottom of the image; the AppleScript code is printed underneath for copying and pasting):

AppleScript code
set [X, Y] to [text 1 thru 30, text 31 thru -1] of (the clipboard)

replace of (a reference to X) from "e" to "E"

set the clipboard to X & Y
return X & Y

to replace of str from A to B
	local str, A, B
	
	set the text item delimiters to {B, A}
	set contents of str to text items of str as text
end replace
1 Like

Hey @troy,

It looks to me like you did a workman like job in post #4 using Keyboard Maestro's available tools.

Most people don't know the Keyboard Maestro Engine adds a very powerful find/replace command to AppleScript.

It makes jobs like this pretty simple.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/08/18 23:01
# dMod: 2018/08/18 23:01 
# Appl: Keyboard Maestro Engine
# Task: Search and Replace in AppleScript with Regular Expression Support Using Keyboard Maestro.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_Engine, @Search, @Replace, @Regular, @Expression, @Support
----------------------------------------------------------------

# Set the clipboard to demo text.
set the clipboard to "Now is the time for all good men to come to the aid of their country."

# Extract specific length strings from the clipboard.
set {dataStrFront, dataStrBack} to {text 1 thru 10, text 11 thru -1} of (get the clipboard)

# Search/Replace using the Keyboard Maestro Engine.
set newDataStr to kmReplace("\\b\\w", "•", dataStrFront, true, true, false) of me & dataStrBack

# Result:

--> "•ow •s •he time for all good men to come to the aid of their country."

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for findPattern replace replacePattern ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmReplace
----------------------------------------------------------------

Now I'll break the script up, so it's a little easier to understand:

----------------------------------------------------------------

# Set the clipboard to demo text.
set the clipboard to "Now is the time for all good men to come to the aid of their country."

# Extract specific length strings from the clipboard.
set clipText to the clipboard
set dataStrFront to text 1 thru 10 of clipText
set dataStrBack to text 11 thru -1 of clipText

# Search/Replace using the Keyboard Maestro Engine.
set newDataStr to kmReplace("\\b\\w", "•", dataStrFront, true, true, false) of me
set newDataStr to newDataStr & dataStrBack

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for findPattern replace replacePattern ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmReplace
----------------------------------------------------------------

Now I'll use find/replace for everything:

----------------------------------------------------------------

# Set the clipboard to demo text.
set the clipboard to "Now is the time for all good men to come to the aid of their country."

# Get the text from the clipboard.
set clipText to the clipboard

# Extract specific length strings from the clipboard.
set dataStrFront to kmReplace("^(.{10}).+", "$1", clipText, true, true, false) of me
set dataStrBack to kmReplace("^.{10}(.+)", "$1", clipText, true, true, false) of me

# Search/Replace using the Keyboard Maestro Engine.
set newDataStr to kmReplace("\\b\\w", "•", dataStrFront, true, true, false) of me
set newDataStr to newDataStr & dataStrBack

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for findPattern replace replacePattern ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmReplace
----------------------------------------------------------------

NOTE - For those who don't know, AppleScripts are run using a Keyboard Maestro Execute an AppleScript action

-Chris

1 Like