Mac String and variable concatenation in AppleScript

tekboi

macrumors 6502a
Original poster
Aug 9, 2006
692
83
EasŦcoast
I'm trying to concatenate at variable within a path name string to make this script more efficient. This is the code I have so far. But I can't seem to find the right syntax needed in order to set the variable in the string.

Code:
  set vserver to POSIX file "/Volumes/johndoe"
    set user to "johndoe" as string

    set source to POSIX file "/Users/johndoe/Documents"

    tell application "Finder"

        duplicate source to vserver with replacing
    end tell
I want johndoe to be replaces by variable user to make the code more maintainable for the future. My ultimate goal is to be able to copy multiple home folders including: movies, pictures, documents etc... onto a shared server.

I tried doing this:
Code:
"/Users/" & user & "/Desktop"

But I received this error:


"Finder got an error: Handler can’t handle objects of this class." number -10010


----------

also, is there a function that will pull the logged in users name, so that I can use that as a variable?
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
2

chown33

Moderator
Staff member
Aug 9, 2009
8,445
4,448
Pale blue comma
  1. Launch AppleScript Editor.
  2. Open the scripting dictionary of StandardAdditions.osax (File > Open Dictionary...)
  3. Under "File Commands", look at the "path to" command.
  4. One of the myriad items you can get the path to is home folder.
So put this in the script window:
Code:
get path to home folder
and run it. Look at the result.

For the Posix path representation:
Code:
get Posix path of (path to home folder)

The 'system info' command (Miscellaneous Commands in StandardAdditions) can provide short user name, long user name, and various other things.
 

jpf55

macrumors newbie
Jun 17, 2011
2
0
"Finder got an error: Handler can’t handle objects of this class." number -10010

I recently tried to create a simple script to open certain apps and windows in Spaces 2.

Apps and network drives were not a problem but opening a folder always gave me the "Finder got an error: Handler can’t handle objects of this class." number -10010" error

Using chown33's suggestion, I tried both
Code:
get path to home folder
get POSIX path of home folder
get path gave me a path with columns instead of slashes which ultimately solved my problem but get POSIX path gave another error "Can’t get POSIX path of home folder." number -1728 from POSIX path of home folder" which I don't have time to research now. here is what works:
Code:
tell application "Finder"
		--open "MacFusion/Users/username/Documents/NewStuff"  DOES NOT WORK
		-- you have to replace all the / by :
	open "MacFusion:Users:username:Documents:NewStuff"	-- WORKS
end tell
Hope this helps you and thank you to chown33
 

chown33

Moderator
Staff member
Aug 9, 2009
8,445
4,448
Pale blue comma
Using chown33's suggestion, I tried both
Code:
get path to home folder
get POSIX path of home folder
The first line (which is the example I posted) is a single-line script. It was intended to illustrate a point, not be a building-block for longer scripts.

The second line is simply the wrong syntax. The example I posted is correct:
Code:
get Posix path of (path to home folder)
You must do it like this, because path to is the actual AppleScript command.

The parenthesized expression results in an alias reference. From that alias reference, one can then get its Posix path property. Here, Posix path is the actual property name.

So if we look at your line of code:
Code:
get POSIX path of home folder
we see that home folder isn't an expression that uses the path to command, and there is no special meaning for home folder as a variable name, so AppleScript rightly complains that it has no idea what you meant.

Some people seem to think that AppleScript can accept any syntax. They are mistaken. There are specific rules for making AppleScript expressions, and it's easy to write things that are meaningless, even if a human would probably understand them. Accuracy is just as important in AppleScript as it is in any other programming language.


here is what works:
Code:
tell application "Finder"
		--open "MacFusion/Users/username/Documents/NewStuff"  DOES NOT WORK
		-- you have to replace all the / by :
	open "MacFusion:Users:username:Documents:NewStuff"	-- WORKS
end tell
The commented-out string doesn't work because the string "MacFusion/Users/username/Documents/NewStuff" isn't a valid Posix path. It isn't a valid pathname for a file alias, either.

Also, even you got the Posix pathname right, you're using a string, not a file alias or a traditional-style Mac pathname (i.e. one using colon delimiters).

Only an alias has a Posix path property. I suggest that you try some simpler tests using file aliases, possibly ones made using the Posix file class. Look it up in the scripting dictionary of StandardAdditions, or google it along with the keyword applescript to find tutorials.

Also, the path to command has a path to documents folder form that returns the current user's home Documents folder. Again, see the scripting scripting dictionary of StandardAdditions, or google it.