String and variable concatenation in AppleScript

Discussion in 'Mac Programming' started by tekboi, Mar 7, 2014.

  1. tekboi macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
    #1
    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:




    ----------

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

    Joined:
    Jun 17, 2003
    #2
    Here's a relevant example: http://alvinalexander.com/blog/post/mac-os-x/applescript-concatenate-strings

    The Google search term I used to find it was "applescript string concatenation". Good luck with your scripting.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    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.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Chown33 already gave all the answers so here's an example.
     

    Attached Files:

  5. tekboi thread starter macrumors 6502a

    tekboi

    Joined:
    Aug 9, 2006
    Location:
    New Orleans ✈ WesŦward
  6. jpf55 macrumors newbie

    Joined:
    Jun 17, 2011
    #6
    "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
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    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.


    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.
     

Share This Page