"The file name is not valid" error when trying to use "Get Group from Matched Text"
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:
- Accepts URLs
- Get contents of [URL]
-
Make HTML from [Contents of URL]
The HTML at this stage is something like this:
<a href="http://www.example.com">Click here</a>
- Match
<a href="([^"]+)">Click here</a>
in [HTML from Rich Text] - Get [Group At Index] [1] in [Matches]
- 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:
-
Text
<a href="http://www.example.com">Click here</a>
- Match
<a href="([^"]+)">Click here</a>
in [HTML from Rich Text] - Get [Group At Index] [1] in [Matches]
- 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?
Solution 1:
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:
- Accepts URLs
- Get contents of [URL]
-
Make HTML from [Contents of URL]
The HTML at this stage is something like this:
<a href="http://www.example.com">Click here</a>
- Match
<a href="[^"]+">(Click here)</a>
in [HTML from Rich Text] - Get [Group At Index] [1] in [Matches]
- 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 wasText
. - 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 theThe 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:
- Accepts URLs
- Get contents of [URL]
-
Make HTML from [Contents of URL]
The HTML at this stage is something like this:
<a href="http://www.example.com">Click here</a>
- Match
<a href="([^"]+)">Click here</a>
in [HTML from Rich Text] - Get [Group At Index] [1] in [Matches]
- Get text from [Text]
- Show [Text] in Quick Look