Applescript create folder on NAS

Discussion in 'Mac Programming' started by Aouttier, Mar 3, 2016.

  1. Aouttier, Mar 3, 2016
    Last edited: Mar 3, 2016

    Aouttier macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #1
    Hello,

    I’ve just switched from Windows to mac (and I’m happy with my decission!).
    Now, I’m trying to write an apple script to make a folder on my external hard disk (NAS). My hard disk is mounted, I can see the following in Finder. The disk is called ‘Diskstation’ (As you can see, I’m dutch speaking)

    [​IMG]

    I’ve looked on the internet, I’ve found a lot of solutions (and tried them), but none seems to work.
    The last thing I tried is:

    Code:
    set DiskName to "DiskStation"
    set FolderName to "Heron"
    tell application "Finder"
        set DoelFolder to folder FolderName of disk DiskName
        make new folder at DoelFolder with properties {name:"test"}
    end tell
    
    Can anyone say me what I’m doing wrong and give me an advice on how to do it?

    Thanks in advance
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    Welcome to the world of the Mac!

    I'm assuming that a folder called "Heron" already exists on the disk DiskStation, and you want to make a new folder inside the "Heron" folder. If so, I'd do it something like this:

    Code:
    tell application "Finder"
        make new folder in alias "DiskStation:Heron:" with properties {name:"test"}
    end tell
    
    You weren't too far wrong and the AppleScript dictionaries can be a bit confusing at times. Personally, I think the Finder's is especially bad. You'll get used to it!
     
  3. Aouttier thread starter macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #3
    I still doesn't work. I even can't compile it.

    [​IMG]

    It says that he can't find 'DiskStation: Heron'. When I change the 'in' in 'at', it's compiling. But, when I execute the script, he can't find the folder.

    What is still wrong?
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    OK. Can you try this as an experiment:

    Code:
    set theFolder to choose folder
    
    tell application "Finder"
        make new folder in theFolder with properties {name:"test"}
    end tell
    
     
  5. Aouttier thread starter macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #5
    It didn't work either...

    I tried a bit further, also some surfing. I tried

    Code:
    set loc to "DiskStation:Heron:"
    tell application "Finder"
        make new folder at folder loc with properties {name:"test"}
    end tell
    But now I get the fault -1728 (The referenced object doesn’t exist. This is a run-time resolution error, such as when attempting to reference a third object when only two objects exist.) I would say that the disk isn't mounted, but I can make a folder in the folder 'Heron' on the Diskstation manually. Thus, that seems not to be the problem...
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #6
    Hmmm.

    Can you try this...

    Code:
    set theFolder to choose folder
    set thePath to (theFolder as alias)
    display dialog thePath as text
    
    ...select your folder where you want to make your folder when prompted, and let me know what the dialog says.
     
  7. Kornelis, Mar 7, 2016
    Last edited: Mar 7, 2016

    Kornelis macrumors member

    Kornelis

    Joined:
    Dec 4, 2010
    Location:
    Amsterdam, NL
    #7
    What are the names of the shares on the Diskstation?

    I have the following partitions/shares: fotografie, multimedia, muziek & opslag.

    So if I want to make a folder called test on my Nas on the share opslag the code must be something like:

    Code:
    tell application "Finder"
        make new folder of alias "opslag:" with properties {name:"test"}
    end tell
    
    Schermafbeelding 2016-03-07 om 15.58.28.png

    Succes
     
  8. Aouttier thread starter macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #8

    It works when I change the code a little bit (without the alias)

    [​IMG]
     
  9. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #9
    Yeah, but that's not really what we wanted to know. What happens when you try to run the code I gave you?

    I'm trying to see what the path to the folder you select is. Since you've removed the "choose folder" bit, it's a bit pointless as a test! ;-)
     
  10. Aouttier thread starter macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #10
    It's driving me crazy...

    When, I execute your code, I got the following error:

    [​IMG]
    (translated: the script isn't found)

    I then tought that the drive wasn't corrected mounted. So, I expanded the script that it also mounted the volume:
    Code:
        if exists disk "192.168.1.107" then
            display dialog "volume is mounted"
        else
            display dialog "volume isn't found"
            mount volume "smb://admin:xxxxxx@192.168.1.107/Heron"
        end if
    end tell
    set theFolder to "192.168.1.107:Heron"
    set thePath to (theFolder as alias)
    display dialog thePath as text
    tell application "Finder"
        make new folder at theFolder with properties {name:"test"}
    end tell
    When, I unmount the disk, and then execute the script, I see that the disk is mounted, and then I got the following error:
    [​IMG]
    I can't figure out what is wrong


    Thank you for helping me
     
  11. Kornelis, Mar 9, 2016
    Last edited: Mar 9, 2016

    Kornelis macrumors member

    Kornelis

    Joined:
    Dec 4, 2010
    Location:
    Amsterdam, NL
    #11
    When a drive is mounted it is part of the file system. If you go in Finder to the folder /Volumes (Finder -> Ga -> Ga naar map... en geef daar in /Volumes).

    In Volumes you will see a volume called Heron when this partition is mounted. If this is the case then you can use the following code to make the folder on your NAS:

    Code:
    tell application "Finder"
        make new folder of alias "Heron:" with properties {name:"test"}
    end tell
    
    Applescript "knows" that the drives (internal drives, network drives, USB drives etc.) are accessible through the /Volumes folder, so you don't have to mention that in the above code, only the name "Heron:" is enough.

    I think you can't access network drives directly through the IP number (like Windows), they first must be mounted to access them. I'm not 100% sure about this.
     
  12. Luke MacWalker macrumors member

    Joined:
    Jun 10, 2014
    #12

    (FYI, error -43 means "file not found")

    I think your problem may be a missing colon after "Heron" when you set theFolder in your script.
    I've just tried this script successfully at home:

    tell application "Finder"
    set newFolder to make new folder at alias "Archives:" with properties {name:"Test folder"}
    end tell

    where "Archives" is the name of a remote volume on my NAS on which I have the necessary rights to write.
    In my case, DiskStation413 is the name of the server (NAS), and "Archives" is the name of one of the volumes shared by this server. By the way I access this server through AFP (not SMB).

    So, in your case, if the volume "Heron" on your "DiskStation" server (NAS) is already mounted, and provided that you have writing rights to this folder, this script should work:

    tell application "Finder"
    set newFolder to make new folder at alias "Heron:" with properties {name:"test"}
    end tell

    If I need to mount the volume first, I can do this:
    mount volume "afp://myUsername:myPassword@DiskStation413._afpovertcp._tcp.local/Archives"
    tell application "Finder"
    set newFolder to make new folder at alias "Archives:" with properties {name:"test"}
    end tell

    I tested it and it works perfectly. I don't know why the more verbose (and more AppleScript style) syntax of mount volume (that is mount volume "Archives" on server "DiskStation413" as user name "xxxx" with password "xxxxxxxx") did not work though. :(
    Anyway, this should work for you too, with the names of your volume and server:

    mount volume "afp://yourUsername:yourPassword@DiskStation._afpovertcp._tcp.local/Heron"
    tell application "Finder"
    set newFolder to make new folder at alias "Heron:" with properties {name:"test"}
    end tell

    Or something similar with SMB instead of AFP if you did not turn on AFP on your NAS (but as you already succeeded in mounting this volume, you should be able to do it as you did it before; if you are interested to know where this ._afpovertcp._tcp.local part came from, just do a "Get info" (command-I) on your Heron volume in the Finder).

    Happy coding!




     
  13. Aouttier thread starter macrumors newbie

    Joined:
    Mar 3, 2016
    Location:
    Belgium
    #13
    NOw, it's working ... with al your tips and a lot of trying.

    I don't really know why it is working now, but I'm happy that it's working!

    Thanks a lot!
     

Share This Page