How do I make a string into a path?

I find a lot about path into string and how to put things in predefined folders like “desktop” but I need to create a new folder and it’s making me nuts.

“job_folder_name” is a string version of the path to a folder just above the level I want to create the new folder. The value in this case is the string 10010

However the first line throws an error "Can’t make “10010” into type constant.

If I leave out “path to” in the first line, I get an error at the line:

set destination_folder to make new folder at PreDestination with properties {name:destination_folder}

‘Can’t make “10010” into type item’

I’m getting frustrated because I can’t find syntax examples that break down what must be an alias, a string, an object, etc. or how to change one to the other. I’ve programmed in several languages but am new to ApplesScript.

---------------------------------------------code portion---------------------

set PreDestination to (path to job_folder_name)

tell application “Finder”
try --if folder doesn’t exist create folder
set destination_folder to folder destination_folder of PreDestination
on error
set destination_folder to make new folder at PreDestination with properties {name:destination_folder}
end try

duplicate hfsPathStripped to destination_folder --source file must be defined as alias - destination folder is alias in HFS form
set CopiedFullPath to destination_folder & ":" & (name of file hfsPathStripped) as alias
--new alias object is concatenation of destination_folder, colon separator, and "name" object of hfsPathStripped which is full path and file name of source
set name of CopiedFullPath to ProposedFileName --the name object of the new HFS full path and file name alias object is set to the new name (a text object)

end tell

Hi,

path to expects a predefined folder name (defined in Standard Additions).
It does not work with a custom name.

PreDestination must contain a full HFS string path e.g.


set PreDestination to "Macintosh HD:Users:myUser:PreDestination:"

tell application "Finder"
	if not (exists folder destination_folder of folder PreDestination) then
		set destination_folder to make new folder at folder PreDestination with properties {name:destination_folder}
	else
		set destination_folder to folder destination_folder of folder PreDestination
	end if
end tell

in the Finder tell block use the keywords folder, file or item to specify Finder objects

A Finder object can be coerced to a HFS string path with as text

Hi mistercat,

Also note that when you do get a path to a folder, it already ends with a colon. So you don’t need to concatenate another colon (i.e. some_folder_path & “:” & new_folder_name). This will error.

gl,

Editted: added quotes to the colon.

Here’s an example:


tell application "Finder"
	try
		(make new folder at desktop with properties {name:"TestFolder"}) -- note that desktop is default
	end try
end tell
set dp to (path to desktop) as string
--> "Macintosh HD:Users:kelhome:Desktop:"
set new_path to dp & ":" & "TestFolder" -- on desktop
--> "Macintosh HD:Users:kelhome:Desktop::TestFolder"
try
	new_path as alias
on error err_msg
	err_msg
end try

gl,

I traced my problem to PreDestination. I had only been using the last part of the HFS path, instead of the full path.

I’m not sure I understand: “A Finder object can be coerced to a HFS string path with as text”
Is a Finder Object in this sense limited to a folder, file or alias?
When I look at AppleScriptFinderGuide.pdf there are lots of other objects like Application Window.


I am still having a problem with string-to-folder. How to “coerce” string to folder?

where hfsPathStripped is the string
“LEIBNIZ:Users:bcrow:Desktop:PDF test file.PDF”

and full_path_destination_folder is the string
“JJSLO:Jobs Direct:10000-10099:10010:10010 SLO sample info”

--------------applescript code----------------------------------------------------------

duplicate hfsPathStripped to full_path_destination_folder

gives me an error:

Can’t make “JJSLO:Jobs Direct:10000-10099:10010:10010 SLO sample info” into type folder"


So, again, the general question: What makes a string into a folder?

"MacHD:path:to:folder:"

is a HFS string path


tell application "Finder"
	set theFolder to folder "MacHD:path:to:folder:"
end tell

is a Finder object specifier.

HFS string path → Finder object : add appropriate specifier keyword (file, folder or item) before the literal string in a Finder tell block
Finder object → HFS string path : add as text to the end e.g.

theFolder as text 

(Finder tell block is not required)

Hi Stefan,

I wasn’t writing about your script, but mistercat’s

I should have quoted that.

Have a good day,

I know, the problem with the quoted line is that destination_folder is a Finder object so the error occurs because destination_folder couldn’t be implicitly coerced to a string and therefore the colon couldn’t be appended anyway

Hi Stefan,

I know that. That’s why I wrote:

I was trying to say that to build a reference, you don’t want to double colon it. But, thanks for clearing things up.

gl,

I have confused things by referring to string when I meant string variable. It does seem to work with literal strings but I have to use string variables because the paths are constructed on-the-fly based on user input.

In this case, I try to convert a string variable made from concatenated strings and string variables:

----------------------------AppleScript code---------------------------------------------------

Set FullRangeFolder_Job_SLO to “JJSLO:Jobs Direct:” & RangeFolder & “:” & job_folder_name & “:” & JobNumber & " SLO sample info" as text

When I display the result FullRangeFolder_Job_SLO displays as:
“JJSLO:Jobs Direct:10000-10099:10010:10010 SLO sample info” Note: without terminal colon

---------------------------applescript code-----------------------

set full_path_destination to FullRangeRolder_Job_SLO & “:” --adds terminal colon
set full_path_destination_asFolder to folder full_path_destination

From what I understand, this should create the folder object full_path_destination_asFolder.
Unfortunately, the second line results in an error.
The last word full_path_destination is flagged and the error message is:

Expected end of line, etc. but found identifier

I get the same error if I do not add the terminal colon in the first line.

Is there some obvious error here?

Update: I had forgotten to put the folder object assignment inside “Tell application”

----------------AppleScript code----------------------------------------------------

set full_path_destination to FullRangeRolder_Job_SLO & “:” --adds terminal colon
tell Application “Finder”
Set full_path_destination_asFolder to folder full_path_destination
end tell


Now I get a run error on the line inside the tell:

Can’t get folder “JJSLO:Jobs Direct:10000-10099:10010:10010 SLO sample”

I think I’ve seen this before. Try changing this:

set full_path_destination_asFolder to folder full_path_destination

to


set full_path_destination_asFolder to (full_path_destination as alias)

I find the last reported error was the result of looking for the wrong level in the folder hierarchy.

I want to thank you two gentlemen for providing several necessary insights to solve my problem.

I am making a special file with your posts for future use.:slight_smile:

To clarify: once I got the right folder specified, it worked fine without the “as alias”.

I have seen “as alias” being necessary in other file/folder manipulations, so I will keep it in mind.

‘as alias’ will error if the item doesn’t exist.

Good to know.
I remember the manuals saying that was a major distinction between alias and file/folder.
Can’t make an alias out of something doesn’t already exist.

For the record: An file path is an string, alias is an file pointer and are persistent like properties.

try
theFile
on error
set theFile to choose file -->returns an alias to the chosen file
end 

now when you move the file between runs and the alias will follow the file, even renaming the file. File paths, because they are strings, won’t follow and aren’t persistent unless you define them as property. There are commands that accept file paths and alias, but there are also commands that allows only 1 of them. For instance when you want to open an document, it’s normal that the open command only allow alias (because you can’t open non-existing files). When you save an document, to an non existing path for instance, you’re forced to use file paths instead.

Hello.

There are several occasions nowadays, that an alias to a non existant file won’t throw and error. (After Leopard). So now you have to really test if the file exists, to be sure to have working code in all cases.

Still, an alias coercion as kel1 mentioned, the file needs to exists otherwise it will throw an ‘not found’ error.

Hello.

I can’t recreate the situation right now, but I tell you, it wasn’t inside a try block. And maybe it wasn’t a direct coercion, but there are times, where an alias to a file that doesn’t exist will not throw an error, after Leopard.

I just can’t recollect the exact context at the moment.

Edit

At least you can’t assume that when something treated as an alias, will throw an error, when the file doesn’t exist.
Say you pass a parameter, into a handler , and treats that parameter as an alias, but the file behind it doesn’t exist, I can’t really describe it better, but such code may fail silently, without throwing an error.