Jump to content
nikivi

Open Alfred file actioning from CLI

Recommended Posts

The Alfred 3.5 pre-release adds new 'browse' and 'action' alongside the 'search' AppleScript commands.

 

Use search to populate Alfred's search field.

Use browse to populate Alfred's file system navigation path.

Use action to show the actions panel for the given file.

 

Cheers,

Andrew

Share this post


Link to post

@Andrew thanks for those new commands! This help to speed things up and reduce workarounds!

 

As for the "action" command, would it be possible to accept list of file paths (ex: 'tell application "Alfred 3" to action { "/path/to/file1", "/path/to/file2" }') so it would pops the Alfred File Action window like if we made multiple file selection in the Finder so we can act on all of them with the same action ? I think this would make the command more feature complete :)

 

For everyone, here is my updated Bash Profile in case this is helpful to someone:

# Alfred Search Function
alfs() {
    if [ $# -eq 0 ]; then   # If nothing is put as arguments open Alfred at the working directory so it list the content
        osascript -e "tell application \"Alfred 3\" to browse \"$(pwd)\""
    elif [ $# -eq 1 ]; then # If only one argument is set
        if [[ -d $1 ]] || [[ -f $1 ]]; then   # if it looks like a path or file, then make sure we send a full path to Alfred
            osascript -e "tell application \"Alfred 3\" to browse \"$(realpath -s "$1")\""
        else    # Any other words that are not a path would be sent to Alfred as is  (ex: alfs snip  ->  would open Alfred with "snip")
            osascript -e "tell application \"Alfred 3\" to search \"$*\""
        fi
    else   # If multiple arguments are set, then they are sent to Alfred as is. (ex: alfs define allo  ->  would pop Alfred with "define allo")
        osascript -e "tell application \"Alfred 3\" to search \"$*\""
    fi
}
# Alfred Action Function (pop the Alfred Action Window)
alfa() {
    if [ $# -eq 0 ]; then    # If no arguments, pop Alfred Action Window in the working directory
        osascript -e "tell application \"Alfred 3\" to action \"$(pwd)\""
    else
        args=""
        argsAreAllPaths=true
        for arg in "$@" ; do
            filePath=$(realpath -s "$arg")
            if [[ -d $filePath ]] || [[ -f $filePath ]]; then    # if the arg is a file of folder, append the path to the args string to build a list for the osascript command
                args+="\"$filePath\","
            else
                argsAreAllPaths=false   # if one argument is not a folder or file path, then pop Alfred with the standard search and try to access Alfred Action Window. This also makes it clear there's a problem with one of the passed paths
                break
            fi
        done
        if [ "$argsAreAllPaths" = true ] ; then    # If all arguments are files or directories, open Alfred Action Window with the list
            args=${args%?} # remove the trailing comma
            osascript -e "tell application \"Alfred 3\" to action { $args }"
        else  # If not all arguments are files or directories, search as is and try to access the Alfred Action Window. The Action Window should pop if it's possible, or the standard Alfred Search would be shown (ex: alfa activity monitor  ->  Would open the file action window of the Activity Monitor)
            actionKey="keystroke (ASCII character 29)"  # (meaning: right arrow) Put your prefered action key (Alfred -> File Search -> Actions -> Show Actions) as applescript command for keystroke or key code (ex: "key code 98")
            delayBetweenEvents=0.1    # Play with the number if the action doesn't work correctly
            osascript -e "tell application \"Alfred 3\" to search \"$*\"" -e "delay $delayBetweenEvents" -e "tell application \"System Events\" to $actionKey"
        fi
    fi
}

 

Edited by GuiB
Updated to allow to pass multiple file paths to the new Action command in Alfred 3.5

Share this post


Link to post
18 minutes ago, GuiB said:

osascript -e "tell application \"Alfred 3\" to browse \"$(realpath "$1")\""

 

realpath resolves symlinks by default. With this command, you might end up deleting the target file instead of the symlink you actually passed as an argument. Should be realpath -s, imo.

Share this post


Link to post

@GuiB Yep no probs, I've updated it to accept one or more files for the next release :) 

 

Also, interesting what @deanishe said - For some reason, I was fully resolving paths and symlinks on the receiving code. I've changed that to just standardise paths instead (i.e. resolve ~/).

Share this post


Link to post

@deanishe I get your concern and updated my script above. However, this made me realize that Alfred commands resolve symlinks and aliases. So, @Andrew this would be something to have a look and I think is more a concern for the new 'action' command since this make the File Action pop the real file instead of the symbolic link or alias.

 

--- Just keeping the rest of this post to give more info on the subject (basically, to let you know that the file name is not always set to the selected file), but @Andrew posted while I was writing and things should be fine with the update that you mention above

 

For, example:

Action command:

  • When activating the File Action using a hotkey in Finder:
    • When selecting a file: we get the file in the file action window with the title set to the file name and the path to the file (that's fine)
    • When selecting an alias: we get the alias in the file action window with the title set to the alias file name and path to the alias file (fine)
    • When selecting a symbolic link: we get the symbolic link file in the file action window with title set to the real file name, but path is the symbolic link path (so not as the others for the name, but this is fine since we work on the symbolic link path)
  • When activating the File Action using the new Action command:
    • We always get the real file path (name is set to the real file name and the path is set to the real file path even if we use the command with a path that is a symbolic link or alias)
Edited by GuiB

Share this post


Link to post

I've updated my script above to pass multiple files to the new Alfred action command (now that it is possible with the new beta version: Build 867)

Share this post


Link to post

Is it possible to use the action command to call a specific file action?

 

For example, when actioning a file could I trigger Alfred's "Open With..." file action?

 

Relatedly, if this is not possible to accomplish this with the action command, is it possible through some other means (i.e., specifically targeting Alfred's Default Actions, not user created ones - that would be easy)?

 

Thanks for your help!

Share this post


Link to post
26 minutes ago, Jasondm007 said:

Is it possible to use the action command to call a specific file action?

 

Not directly, no, but after telling Alfred to action a file, you can simulate keypresses to enter the name of the action you want and then simulate a ↩ keypress:

tell application id "com.runningwithcrayons.Alfred" to action "~/Desktop"
delay 0.1  -- give Alfred time to open
tell application "System Events"
    keystroke "Open With"
    key code 36  -- simulate ↩
end tell

 

Edited by deanishe

Share this post


Link to post

Thanks a ton, @deanishe!! I was trying to avoid going down the GUI route - because I have a few similarly named file actions that I've created - but perhaps I'll just rename them and use your method.

 

@Andrew Out of curiosity, has there ever been any discussion about allowing this though the action command?

 

Relatedly, I've always thought it'd be nice to have a workflow object that operates like the "Call External Trigger" with a dropdown where the user could select a given file action to execute on the output (e.g., default actions, and ideally user created file actions, too) 🙏

Share this post


Link to post

@Jasondm007, yes that would be great to be able to call directly the File Action, but at the moment I think the only way is doing GUI scripting using AppleScript like @deanishe said or you can implement your own version of the built-in actions using a shell script or AppleScript.

 

For the Open With, if you want to avoid the GUI scripting route, you can easily do it using a shell script like:

 

open -a "ApplicationName" /Path/To/File

 

Share this post


Link to post

@GuiB Thanks for the suggestion. I was actually trying to trigger Alfred's "Open with" file action directly (or any other file action, for that matter). I wanted to attach a global shortcut to it (because I hate right-clicking things and bumbling around with the default "Open with" contextual menu in Finder and other apps). Alfred's file action for that is much easier for me to see and it avoids the mouse. Thanks again!

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...