gurkensaas
gurkensaas

Reputation: 913

Applescript can't set name of file

So I'm trying to rename a file using Applescript. I searched it up and apparently the command is set name of file theFile to theString. I tried it and it didn't work. I got the error -10006, which (according to https://www.osstatus.com) is either errAEWriteDenied, errOSACantAssign or telCAUnavail, but I think it's probably the first one. Before you ask, I am the administrator of my machine and path to desktop links to the own Desktop to which I 100% have access. I don't know if that matters (it really shouldn't), but my Desktop is stored in my iCloud.

set thePath to the POSIX path of (path to desktop)
set theName to "hello world.txt"
set theFile to thePath & theName
set the name of file theFile to "hello.txt"

Upvotes: 0

Views: 741

Answers (1)

vadian
vadian

Reputation: 285064

Only the Finder or System Events is able to set the name. In Finder it’s pretty easy because the desktop of the current user is the root folder.

tell application "Finder"
    set theFile to "hello world.txt"
    set theName to "hello"
    set the name of file theFile to theName & ".txt"
end tell

By the way, the Finder doesn’t know about POSIX paths, it prefers HFS paths (colon separated)

Upvotes: 2

Related Questions