Make new folder with System Events

I want to make a folder at a path in a string, like so;

property presetFolder : (((path to application support from user domain) as Unicode text) & "ReportHours:Presets:")

try
			
			set presetFiles to list folder presetFolder
			
		on error
			try
				tell application "System Events" to make new folder at (path to application support folder from user domain) with properties {name:"ReportHours"}
			end try
			try
				tell application "System Events" to make new folder at (((path to application support folder from user domain) as Unicode text) & "ReportHours:") with properties {name:"Presets"}
				set presetFiles to list folder presetFolder
			end try
		end try

So for some reason I’m hellbent on not using Finder.

It seems more elegant to use System Events. But it also seems impossible.

Will one have to settle for a “do script” statement?

Thanks a lot!

/Fox

Hi,

it’s recommended, to use never a path to statement in a property,
because it could cause problems on other machines.

try this


set presetFolder to ((path to application support from user domain as text) & "ReportHours:Presets:")
do shell script "mkdir -p " & quoted form of POSIX path of presetFolder

Thanks, that’ll work wonderfully!

Just out of curiosity though; I feel rather confused about paths in AS. From what I’ve been able to read in the references they can be aliases, file references (which are, if I remember correctly, of the data class and has to be coerced into text to be read/modified by AS, whether they are still file references after this coercion is also unclear). Then there is the object form ‘file “this” of folder “that”’ and now it seems on your code that any string containing a path (file reference string?) has a POSIX path property.

At times I also find it hard to tell when this string path becomes a file or folder object and what I can do with that. (As that seems to differ between the applications receiving the command.)

Any good reading to sort this out?

It is quite a challenge to get System Events to make a new folder. It can do it, but the dictionary is of very little help. I found this secret online recently (no thanks to Apple who has done exceptionally little to document the use of System Events since it was released in Mac OS 10.2). The correct syntax to use is:

Tell Application “System Events” to make new folder at end of (alias) with properties (properties)

Here’s an example:

tell application "System Events"
	make new folder at end of (path to desktop) with properties {name:"My New Folder"}
end tell

This almost seems like a bug to me, since there’s no indication that (or why) you need to include “end of” when addressing the target alias, but at least it does work.

1 Like