lipatovroman Posted May 11, 2023 Share Posted May 11, 2023 Hello! I need to remove hyphens from phrases by keyword. For example, we-have-this-phrase. I need to select it, press some keyword and get: we have this phrase. How to get this behavior? Thanks. Link to comment
Stephen_C Posted May 11, 2023 Share Posted May 11, 2023 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
lipatovroman Posted May 11, 2023 Author Share Posted May 11, 2023 Thank you Stephen. I'm a newbie. How to add hot key to this script? For example, I highlighted the text, then pressed cmd+alt+c and the phrase was replaced without the hyphens. Thanks. Link to comment
Stephen_C Posted May 11, 2023 Share Posted May 11, 2023 (edited) 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 May 11, 2023 by Stephen_C Link to comment
lipatovroman Posted May 11, 2023 Author Share Posted May 11, 2023 (edited) I apologize, I expressed my thoughts incorrectly at the beginning of the thread. I need: 1. highlight the text 2. press hot key 3. get same text without hyphens (paste instead old text) I have a similar script, with changes symbols case. https://transfer.sh/G3dAzc/Case Converter 2.alfredworkflow I need the same behavior. Edited May 11, 2023 by lipatovroman Link to comment
Stephen_C Posted May 11, 2023 Share Posted May 11, 2023 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
lipatovroman Posted May 11, 2023 Author Share Posted May 11, 2023 Ok, I replaced first block to hot key and enabled Automatically paste to frontmost app https://transfer.sh/Yh7bbG/Convert hyphenated phrase.alfredworkflow But it's not working for me. I highlighted the text, then pressed my hot key - and nothing happened. What I made wrong? Link to comment
Stephen_C Posted May 11, 2023 Share Posted May 11, 2023 Double click on the hotkey action at the start of the workflow and under Hotkey Settings change Argument: to Selection in macOS. Stephen lipatovroman 1 Link to comment
lipatovroman Posted May 12, 2023 Author Share Posted May 12, 2023 YES!!! Thank you Stephen! After processing script adds new empty line... Link to comment
Stephen_C Posted May 12, 2023 Share Posted May 12, 2023 Good: I'm glad it's working for you. Stephen Link to comment
Stephen_C Posted May 12, 2023 Share Posted May 12, 2023 1 minute ago, lipatovroman said: After processing script adds new empty line. OK - you should be able to delete that by adding, after the script action and before the copy to clipboard action, the Transform action set to Trim Whitespace. Stephen lipatovroman 1 Link to comment
lipatovroman Posted May 12, 2023 Author Share Posted May 12, 2023 (edited) Thank you so much! Edited May 12, 2023 by lipatovroman Stephen_C 1 Link to comment
lipatovroman Posted May 17, 2023 Author Share Posted May 17, 2023 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
Stephen_C Posted May 17, 2023 Share Posted May 17, 2023 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
Stephen_C Posted May 18, 2023 Share Posted May 18, 2023 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 lipatovroman 1 Link to comment
macrospect Posted March 10 Share Posted March 10 I'm looking for a similar workflow (replacing spaces with hyphens)! But the links to this one have expired. Would it be possible for a new link to it to be posted? Thank you. Link to comment
Pearcen Posted March 10 Share Posted March 10 (edited) @macrospectTry this one I just threw together which uses no code. At the moment it's replacing - with space from a selection and pastes over the selection, but you can change it in the replace block and whatever characters you feel like. https://github.com/pearcenuk/Alfred-public/raw/main/replace dashes with spaces.alfredworkflow Edited March 10 by Pearcen Link to comment
Stephen_C Posted March 11 Share Posted March 11 @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
Stephen_C Posted March 11 Share Posted March 11 @macrospect I've had a little more time to look at this and I think you'll find Pearcen's workflow will do exactly what you want because it does exactly what I mentioned in my previous post and is much more elegant than my earlier programming solution. Stephen Pearcen 1 Link to comment
macrospect Posted March 11 Share Posted March 11 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. Pearcen 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now