1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Applescript - Losing my mind..

Discussion in 'Mac Programming' started by yellow, Jul 12, 2007.

  1. Moderator


    Staff Member

    I've finally lost it.. I just don't understand what I've done wrong, and of course I'm under a time crunch.

    Here are the basics of what I'm trying to do...

    Because most of the people in this set of labs have personal Macs that don't follow our user naming schemes for AD, I am trying to create a generic mounting script that will (if the file doesn't exist) ask for the user's name and write it to file as text. Then the same script can look at the script for the user's AD username and then mount the drives/shares as appropriate using the correct username, no matter what their current actual user name is..

    The issues that I keep encountering are this:

    1) When the result is written to a text file, it's not straight text. It's full of control characters. So, "joe" is "^@j^@o^@e". Is there some way that I can simply output it as straight text?

    2) I put in safeguards to ask what the script reads from the file always looks right. But in the portion of the script that takes the result from the file and slaps it into a variable that I use as the username in the mount portion of the script
    mount volume "cifs://DOMAIN;"& _placeUsername_ &";share.domain.edu/share$"
    never populates. And when I use the same variable to try and open the user's home folder, it doesn't work (well, it works maybe 1 in 50 times, oddly).

    set mountHome to make new Finder window to folder _placeUsername_ of disk "SHARE$"
    			set current view of mountHome to list view
    BUT, before each of these actions, I ask for the output of the variable and it always works. And when I look at the event logs in Applescript, it's always right.

    mount volume "cifs://DOMAIN;joe@share.domain.edu/share$"
    	make new Finder window to folder "joe" of disk "SHARE$"
    Finally.. as a test, I edited the file with a text editor and simply changed it to "joe".. and what do you know, it works. Sort of.. there appears to be some sort of carriage return/newline, invisible to me. Because the event log outputs like this:

    mount volume "cifs://DOMAIN;joe
    		file "SHARE$:"
    	list disks
    		{"Hanso", "Others", "Losties", "Dharma", "Network", "SHARE$"}
    	display dialog "(view2) got username from file created: joe
    		{button returned:"OK"}
    	make new Finder window to folder "joe
    " of disk "IMMU_HOME$"

    Soooo.. Just what am I hoping that you can you, more knowledgeable folks, can help me with?

    Can I write straight text to a file from Applescript? No control characters?
    Am I supposed to be writing as Unicode?
    Do you see a way around my problem here?
  2. macrumors G3


    All righty ^@ is nul, so it looks like you're getting wide characters when you're expecting to work with 8-bit characters in your commands. You might try dropping the Unicode part to see if that makes the trouble go away, but what does the part that collects and writes the lusername look like (or am I staring right at that part in your post and missing it)?

    The extra CR is probably just an artifact of the text editor you're using.
  3. Moderator


    Staff Member

    It occured to me that it might be useful if you saw how I was gathering the text. (correctly so, too :))

    	alias _pathToFile  -- This is just looking for the existance of the file.
    on error
    	display dialog "Please enter your UserID" default answer "Enter UserID here"
    	set resultOfUserID to (text returned of result)
    	set fileName to (path to documents folder as string) & ".HomeMountUsername-DoNotDelete.txt"
    	set fileID to open for access file fileName with write permission
    	write resultOfUserID to fileID
    	close access fileID
    I'm not using Unicode at the moment. Frankly I didn't know if using it might make this issue go away.

    The CR problem appeared when I use vi or pico. But when I "more" the file, it doesn't appear at all.
  4. macrumors G3



    write resultOfUserID as string to fileID
  5. Moderator


    Staff Member

    You are the best. Best, best, best. I'll throw a robotic mouse for you anytime.
  6. macrumors G3


    Yay. The similarities between AppleScript and INTERCAL are too many to be mere coincidence, aren't they?
  7. Moderator


    Staff Member

    INTERCAL (wikipedia to the resuce)... it strikes me as a programming language as written by Terry Pratchett! :D

    Or perhaps Doug Adams.. but I just finished re-re-reading a TP book, so I have Pratchett on the brain.
    Hopefully this Cipro will clear that up.

Share This Page