The Sloth
The Sloth

Reputation: 387

Copy folder at current path with user generated name (applescript)

I'm trying to set up an applescript that

  1. Asks the user for a text input
  2. Creates a new folder with the input data
  3. Copies files into this folder from another folder

This is what i have so far but i'm getting the error : "File /Users/*****/Desktop/Temp/_scripts/ wasn’t found"

tell application "Finder"
    set newfoldername to text returned of (display dialog "Project name:" default answer "no name")
    set loc to container of (path to me) as alias

    set newclient to make new folder at loc with properties {name:newfoldername}
    set structure to ((POSIX path of loc) & "_scripts/") as alias

    duplicate folder structure to loc

end tell

the _scripts folder is located in the same folder as my applescript. Is it is expecting a file rather than a folder?

Upvotes: 1

Views: 79

Answers (2)

Sunny Pun
Sunny Pun

Reputation: 726

Vadian's Answer has explained why OP's code doesn't work as expected and already has given a working solution.

For someone else who may need an alias for reuse, we may also convert the file path format by explicitly saying "POSIX file" before the path string:

set structure to POSIX file ((POSIX path of loc) & "_scripts/") as alias

(Since the script should copy to newclient so the last line is also modified,) full code here:

tell application "Finder"
    set newfoldername to text returned of (display dialog "Project name:" default answer "no name")
    set loc to container of (path to me) as alias

    set newclient to make new folder at loc with properties {name:newfoldername}
    set structure to POSIX file ((POSIX path of loc) & "_scripts/") as alias

    duplicate folder structure to newclient

end tell

P.S. it is also recommended to check the existence of the _scripts/ folder and non-existence of the folder to be created if they are not controllable.

Upvotes: 1

vadian
vadian

Reputation: 285082

The most significant mistake is that the Finder does not recognize POSIX paths.

If you want to copy the folder "_scripts" on the same level as the running script to the new created folder simply use the Finder specifier syntax (folder "_scripts" of loc)

tell application "Finder"
    set newfoldername to text returned of (display dialog "Project name:" default answer "no name")
    set loc to container of (path to me)

    set newclient to make new folder at loc with properties {name:newfoldername}
    duplicate folder "_scripts" of loc to newclient

end tell

Upvotes: 1

Related Questions