“The file name is not valid” error when trying to use “Get Group from Matched Text”

shortcuts-app

I have a shortcut which is invoked via an action sheet when sharing a URL.

When invoked, it eventually gets the HTML of the webpage via:

  1. Accepts URLs
  2. Get contents of [URL]
  3. Make HTML from [Contents of URL]

    The HTML at this stage is something like this:

    <a href="http://www.example.com">Click here</a>
    
  4. Match <a href="([^"]+)">Click here</a> in [HTML from Rich Text]
  5. Get [Group At Index] [1] in [Matches]
  6. Show [Text] in Quick Look

Step 5 is inserted for debugging purposes. However, instead of showing me the URL it was able to parse, it shows me a page saying:

The file name is not valid


To debug the issue, I tried removing steps 1-3, and replacing it with text:

  1. Text

    <a href="http://www.example.com">Click here</a>
    
  2. Match <a href="([^"]+)">Click here</a> in [HTML from Rich Text]
  3. Get [Group At Index] [1] in [Matches]
  4. Show [Text] in Quick Look

Now Quick Look suddenly does work. I.e. it shows me:

http://www.example.com

What is going on here, and why can I only get it to work with static text and not an action sheet?

Best Answer

Another way I debugged this issue was I changed what it was matching. E.g. instead of matching the URL, I decided to match the "Click here" to see what would happen:

  1. Accepts URLs
  2. Get contents of [URL]
  3. Make HTML from [Contents of URL]

    The HTML at this stage is something like this:

    <a href="http://www.example.com">Click here</a>
    
  4. Match <a href="[^"]+">(Click here)</a> in [HTML from Rich Text]
  5. Get [Group At Index] [1] in [Matches]
  6. Show [Text] in Quick Look

Sure enough, this worked. Quick Look showed me:

Click here

This made it clear that there is something going on with text that happens to be in the format of a URL in Quick Look.

The Get Type (along with Quick Look right after it) was helpful in debugging and figuring out what the issue was.

There were a few non-obvious things going on here:

  • When testing as a shortcut, the type of the text before reaching Quick look was Rich text. When testing with the Text action, the type of the text before reaching Quick look was Text.
  • If Quick Look receives Text (even if it looks like a URL), it'll display the URL as plain text.
  • If Quick Look receives text that looks like a URL of type Rich text, then it will try to display the contents of the URL, as though it's a local file (or something like that). This will most likely result in the The file name is not valid error mentioned above.

The fix, then, is to force the contents to be converted into plain text by adding an extra step right before the Quick Look:

  1. Accepts URLs
  2. Get contents of [URL]
  3. Make HTML from [Contents of URL]

    The HTML at this stage is something like this:

    <a href="http://www.example.com">Click here</a>
    
  4. Match <a href="([^"]+)">Click here</a> in [HTML from Rich Text]
  5. Get [Group At Index] [1] in [Matches]
  6. Get text from [Text]
  7. Show [Text] in Quick Look