Jump to content

Open Alfred file actioning from CLI


Recommended Posts

@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
Link to comment
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.

Link to comment

@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
Link to comment
  • 2 years later...

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!

Link to comment
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
Link to comment

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) 🙏

Link to comment

@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

 

Link to comment

@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!

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