Jump to content

How to create loops?


Recommended Posts

I would like to split a text at every tab character and then paste every splitted item into the frontmost app with a following Key Combo. I can do it with a long list of clipboard actions to paste {var:split1}, {var:split2} and so on. It's okay with limited number of items. But I want it to be more flexible, so it works with every amount of tab chars in the text. For this, I need to connect the last "Arg and Vars" again with the first condition which checks, if there is text left for processing. Alfred seems not to allow this loop. Have I missed a loop feature or does Alfred actively block loop creation?

Link to comment

You can get creative with the External Trigger and Call External Trigger Output to create a loop. But be careful to not get yourself into an infinite loop; be sure to have a Conditional that you know will eventually exit.

 

47 minutes ago, Stephen_C said:

I'm sure this could be done fairly simply with a little AppleScript script and a Run Script action

 

But that’s the way to go, agreed. Though instead of AppleScript I’d recommend its JavaScript for Automation (JXA) counterpart.


You can emulate key combos with /usr/bin/osascript (JavaScript) with the following syntax:

 

// Write text
Application("System Events").keystroke("Something to write")

// Press Esc key
Application("System Events").keyCode(53)

// Press ⌘⇧⇥
Application("System Events").keyCode(48, { using: [ "command down", "shift down" ] })

 

To get the the key codes for specific keys, there is a workflow.


To get your loop, split into Arguments instead of Variables then use in the code:

 

function run(argv) {
  argv.forEach(() => {
    // CODE HERE TO PRESS KEYS

    // Give it one second for the underlying app to react to the key presses
    // Increase or decrease (e.g. 0.5) if necessary
    delay(1)
  })
}

 

48 minutes ago, Stephen_C said:

although AppleScript tends to be looked down upon in this forum so I'll probably not be very popular for suggesting it.

 

You’re too helpful and kind to not be popular!


You’ve mentioned the AppleScript point before, so I want to clarify: there’s nothing wrong with using AppleScript for the things it‘s good for. But for a myriad of reasons (explained in other posts) AppleScript is not a good first language for a beginner who wants to learn to program. That is key. If you like AppleScript, use it, but you should be aware that what you can do with it has a ceiling and (crucially) what you learn is harder to transfer to another language (when you eventually need to) than if you had started with something else.

Link to comment

If I have understood correctly what you want there is actually a rather simple way to do this on the basis of an Alfred Universal Action.

  1. Start the workflow with the Universal Action named as you wish and checked as showing only for Text.
  2. Link that to the Replace action. Set that to regex. In the first box type \t (to find the tab character) and in the second box type \r (or \r\r if you want two carriage returns).
  3. Finally link the Replace action to the Copy to Clipboard Action, if you want, checking Automatically paste to frontmost app. Of course, if you don't want to overwrite the original text you won't do that last step.

Sorry if I have misunderstood, in some way, what you want to do but I hope this helps you to achieve what you want.

 

Stephen

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...