Problem creating new folder in user's preferences

Greetings all,

I’m having a problem creating my own folder in the user’s preferences folder. My first attempt was as follows:

on ConfirmPrefsFolderExists()
	tell application "Finder"
		if (not (exists item prefsPath)) then
			make new folder at preferences folder with properties {name:prefFolderName}
		end if
	end tell
end ConfirmPrefsFolderExists

That got an error of “Finder got an error: Can’t make class folder”. My second attempt was as follows:

on ConfirmPrefsFolderExists()
	tell application "Finder"
		if (not (exists item prefsPath)) then
			make new folder at desktop with properties {name:prefFolderName}
			move item (desktopPath & prefFolderName) to home folder
		end if
	end tell
end ConfirmPrefsFolderExists

That got an error on the move line of “Finder got an error: Handler can’t handle objects of this class”, and the whole line was highlighted. Anyone have any ideas as to what’s going wrong? I can create folders at the desktop with no problems, as highlighted by the success of the first line of the second attempt. Is there some sort of permissions issue? I’ve created files in my folder inside the preferences folder when I created my folder by hand, so it seems to be something with creating a folder.

BTW, the lead-in code is as follows at the top of my script:

property prefFolderName : "StoweMorris"
global desktopPath
global prefsPath
set desktopPath to ((path to desktop) as string)
set prefsPath to ((path to preferences folder) as string) & prefFolderName

Thanks for your assistance!

-Joe

Hi Talix

Try this

set prefFolderName to "StoweMorris"
set prefsPath to ((path to preferences folder) as alias)
tell application "Finder"
	if (not (exists folder prefFolderName)) then
		make new folder at prefsPath with properties {name:prefFolderName}
	end if
end tell

pidge1 - thank you! That worked. So the only reason it wasn’t working was because I wasn’t referring to the preferences path as an alias? If that’s true, why does the “desktop” work but the “preferences folder” or “home folder” doesn’t? I’m just looking to understand so I know better for next time.

Regardless, thanks again for your help! :slight_smile:

-Joe

Hi talix

This also works

set prefFolderName to "StoweMorris"
set prefsPath to (path to preferences folder)
tell application "Finder"
	if (not (exists folder prefFolderName)) then
		make new folder at prefsPath with properties {name:prefFolderName}
	end if
end tell

When i’m inputting the path to a folder i use “alias” by force of habit and inexperience
testing this again it doesn’t need either “string” or “alias”
i think if you change preferences to home it should work aswell, from what i hear on this site, theres quite a lot of places on your mac
which applescript understands the “path to” code.
At this point i would ask anybody on this forum who has alot more experience than me(which isn’t too hard!) to step in
and maybe make this abit clearer for me and talix thanks…

Hope this helps though talix…

Pidge1;

I’m like you - rather than learn where alias is needed and where not, I generally use that form. It is the form returned by Choose Folder, and that generally works. I’m sure there are rules, here, but I don’t know them. I do know that if you’re in the Finder you either have to use its form (document … of …) or an alias and that appending “as alias” to a finder reference will convert it for you.

Hi guys.

There’s just a small snag with the suggested approach, in that Finder checks the desktop for the existence of the folder and, if it doesn’t exist there, tries to make a new one in the prefs folder. So the second time the script is run, it will probably error.

It would therefore be better to specify the prefs folder for both the checking and making operations:


set prefFolderName to "StoweMorris"
tell application "Finder" to tell folder (path to preferences folder) to if not (exists folder prefFolderName) then make new folder at it with properties {name:prefFolderName}


I always use this:

	
tell application "Finder"
	set letterypeMapLokaal to ((folderBibliotheek & "Fonts:") as string)
	if (not (exists folder (letterypeMapLokaal))) then make new folder with properties {name:"Fonts"} at folderBibliotheek as alias
end tell

The first variable ‘letterypeMapLokaal’ is a string (otherwise you always get an error if you want to make it a alias if it’s not existing)