Jump to content

Check whether selection in macOS via Hotkey is a file path or a string or URL


Recommended Posts

How can I check whether the selection in macOS via Hotkey is a file path, a string, or a URL? I've tried using an Automation task with Is_file?, but it's not effective when passing in a string; it doesn't return any result.

Link to comment

I'd be inclined to try this:

  • pass the input to the "Is File?” Automation Task and if the resuilt is true deal with the file;
  • if the result is false pass the input to a Conditional and use regex to check whether the string starts with "http”.

That deals with files and URLs and otherwise (I think!) leaves you with a string. Do you think that might work for you?

 

Stephen

Link to comment

Thank you for your suggestion, but I think it's only a temporary solution, as I want to create a workflow for Sharing that distinguishes between files, strings, and URLs. For example, in the case where I select a string that accidentally starts with 'http' or a URL with 'www.', it wouldn't meet my needs. Not to mention, if I pass in a string like 'A B C', the Automation task (Is_File?) returns an error like this (even when I've processed to join strings and remove spaces):

[14:42:12.368] Sharing[Arg and Vars] Passing output 'A B C' to Automation Task
[14:42:12.370] ERROR: Sharing[Automation Task] Task Argument Error: Expecting single or multiple inputs. Warnings: (
    "Passed in file 'A B C' does not exist, discarding"

Link to comment
2 hours ago, ThanhD said:

Not to mention, if I pass in a string like 'A B C', the Automation task (Is_File?) returns an error like this (even when I've processed to join strings and remove spaces):

[14:42:12.368] Sharing[Arg and Vars] Passing output 'A B C' to Automation Task
[14:42:12.370] ERROR: Sharing[Automation Task] Task Argument Error: Expecting single or multiple inputs. Warnings: (
    "Passed in file 'A B C' does not exist, discarding"

That is because you passed in a string to that Automation Task and it correctly rejects it because it is not a file. If you test the output of the Automation Task in a Conditional true/false test you can filter the input accordingly. In other words what I originally suggested will work: you just have to test the output from the Automation Task in a Conditional in your workflow.

 

Frankly, the easiest way to distinguish between files, strings and URLs is to use Universal Actions and set up a different action for each type.

 

Stephen

Edited by Stephen_C
Typo
Link to comment

Not Action in Alfred, but Universal Action. You use it as the trigger, not as the result.

 

To the conversation above, the Is File? Automation Task is not meant to check if a random string is a file, it checks if an existing path is a file as opposed to a folder. To check if a given path exists you could use the File Conditional. But Universal Actions, as per @Stephen_C’s suggestion, are what I’d recommend too. That way you don’t have to worry about checking what the type is, Alfred does it for you.

Link to comment
5 hours ago, ThanhD said:

my idea is to use a Hotkey to trigger a custom Universal Action via the Alfred Action

I've looked at your workflow but I still don't understand why you want to try to do that. The idea of a Universal Actions is that it works—using your Universal Action hotkey—on a selected item (i.e., text, a fle or a URL). All you need to do is to have a number of Universal Actions within your workflow—each limited to either file, text or URL and labelled appropriately. That means you'll end up with something like that following Universal Actions within the workflow:

  • Send text…
  • Send a URL…
  • Send a file…
  • Share text…
  • Share a URL…
  • Share a file…

Then all you would have to do would be to select something, use your Universal Action hotkey and start typing either "send…” or "share…” to see your relevant Universal Action. Alfred will then take care, for you, to select the relevant action (i.e., on text, a URL or a file).

 

To emphasise the point, you need a Universal Action for each type of object (file, URL or text) and for each action (send or share). At the moment you have Universal Actions which apply to both text and a URL (although possibly in your case it won't make a difference to the recipient if you send a URL as simply text).

 

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