Jump to content

Text processing by keyword


Recommended Posts

I've just run up a very quick, basic workflow (here on transfer.sh) that should do what you want. There's no icon and no explanation or user configuration but you can either type the keyword followed by space and the hyphenated phrase or (rather more efficiently) use it as a Universal Action on selected text. In either case the result is simply copied to the clipboard. The text processing is done using AppleScript.

 

I'm sure you can improve on the workflow with a little work but that should give you some ideas.

 

Stephen

Link to comment

I'd use the Universal Action rather than a hotkey. Is there any reason you can't use that? Both the UA and the hotkey would act on selected text. Given that, is there any reason why you'd want to create a specific hotkey for the workflow? (Sorry if I'm missing something obvious!)

 

Edit: To be clear, select text, use your Universal Action hotkey (set in Alfred Preferences → Features → Universal Actions) and search for the name of the Universal Action given in the workflow (Convert hyphens to space).

 

Stephen

Edited by Stephen_C
Link to comment

Aside from the fact that you're using Python and I used AppleScript I don't think there's any fundamental difference between what you're doing and what I have done except:

  • You're copying to the frontmost app at the end of your workflow. So modify mine by double-clicking on the Copy to Clipboard action and checking Automatically paste to frontmost app.
  • I've used a Universal Action instead of a hotkey. The workflow would work fine if you were simply to add a hotkey instead and link that to the script action.

I hope I have not misunderstood anything you've said.

 

Stephen

Link to comment

Hello Stephen.

Here is your code

on run argv
  set thePhrase to item 1 of argv
  set tid to AppleScript's text item delimiters
  set AppleScript's text item delimiters to "-"
  set theWords to the words of thePhrase
  set AppleScript's text item delimiters to " "
  return theWords as string
  set AppleScript's text item delimiters to tid
end run

 

How to add also replacing for _ symbol?

Thus, script will replace these symbols: -  _

Link to comment

I'm afraid I've not had much time just now so have run this up in some haste - but it does seem to work. Try using this script in place of the previous one:

 

on run argv
set thePhrase to item 1 of argv
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "-"
set theWords to the words of thePhrase
set AppleScript's text item delimiters to " "
set theNewText to theWords as string
set AppleScript's text item delimiters to "_"
set theWords to the text items of theNewText
set AppleScript's text item delimiters to " "
set theNewText to theWords as string
set AppleScript's text item delimiters to tid
return theNewText
end run

 

Stephen

Link to comment

This code will achieve the same and, although it appears longer and more cumbersome, I prefer it in terms of neatness:

 

on run argv
  set thePhrase to item 1 of argv
  set tid to AppleScript's text item delimiters
  set theNewText to my replaceItem(thePhrase, "_")
  set theFinalText to my replaceItem(theNewText, "-")
  set AppleScript's text item delimiters to tid
  return theFinalText
end run

on replaceItem(theText, theItem)
	set AppleScript's text item delimiters to theItem
	if theItem = "_" then
		set theWords to the text items of theText
	else
		set theWords to the words of theText
	end if
	set AppleScript's text item delimiters to " "
	set amendedText to theWords as string
	return amendedText
end replaceItem

 

Stephen

Link to comment
  • 9 months later...

@macrospect I've not had a chance to look at @Pearcen's workflow but if by any chance it doesn't do what you want I should be able easily to recreate and post a link to my workflow. However it should also (as Pearcen implies) be possible simply to replace a space with a hyphen without using code and making use of Alfred's Replace utility. You could easily create a Universal Action (limited to text) to work that magic on appropriate selected text.

 

Stephen

Link to comment

Yes, perfect fix! @Pearcen's workflow does exactly what the original poster wanted (it switches dashes/hyphens to spaces in selected text using a universal action, and I just reversed it to switch spaces to hyphens for my own purposes). In case the link ever breaks, here's how to reconstruct it as a manual workflow: 

 

Trigger: Universal action and/or hotkey (with argument "Selection in MacOS")

Utility: Replace regex - with  ␣ [I reversed this for the conversion of space to hyphen instead]

Output: Copy to Clipboard (with paste to frontmost app checked) [I also checked make transient on clipboard]

 

Thank you so much, Pearcen and @Stephen_C! You saved me a ton of time.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...