Classic "can't make file into type alias"

I have code which searches for files in a given folder. Here is a snippet:

set Folder_Path_dequoted to items 2 thru -2 of Folder_Path as string
set Folder_Path_alias to POSIX file Folder_Path_dequoted as alias
tell application "Finder"
	set search_for to files in Folder_Path_alias where name contains find_filename

etc. etc.

The code works. However, if the given folder name has a single quote in it, I get this error:

Why would a single quote in the folder name stop AppleScript converting the posix name into an alias ?


Can you explain what the first line of your code is trying to achieve?

As it stands, it’s coercing a list to a string, the result of which will depend very much on what text item delimiters are set to at the time. Unless you’re setting them in code you haven’t posted, there’s a fair chance you don’t know what they are.

You also use “items”. Why?

Sorry I’m so obtuse here. The first line is removing single quotes which surround the variable value. The quotes are required earlier on in the script but must be removed here. I believe it’s treating each character as an ‘item’, removing the first and last and thereby removing the quotes. Text item delimiters are the default (ie. nothing). I’ve logged the variable values before and after with this result:

I’ve changed “items” to “characters” in the first line but get the same results.**

If I remove the single quote from the folder name there is no problem. So, in this example:

and the Finder search results in:

Which is the correct result (as there are no files matching).

You are right, I don’t know something about what’s going on. I tried to simulate this patch of code in a small script but, didn’t get the error.


** I used “items” because I found some example code, tried it and it worked for the last 12 months…well…until I hit it with something unusual.

It’s much safer to use text 2 thru -2 of Folder_Path. Then there’s no coercion and risk with delimiters (and it’s more efficient because there’s no intermediate list).

But what you’re showing there is not just a quote in a name, but a single quote followed by a double quote. Which is a pretty odd thing. So if I had to guess, I’d say the problem is possibly happening in code before that you have posted. Your reference to quotes being needed earlier in the script but being removed here adds to my suspicion.

Shane, many thanks. Problem seems to be fixed.

I needed the quoted form for the path name as it goes into do shell script osascript in a number of places. The fix was to not make the quoted form apply to all situations which meant there was no need to remove quotes in this section. I then just manually change to the quoted form in the calls to the do shell scripts.