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.
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)
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
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.
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
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”
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.
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
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:
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
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.
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”
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.
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.
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.