You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

create folder using XML

Can I use a XML file or txt file to create a folder on a certain place? Like:


on drop file.txt on applet

Tell finder

create folder with foldername (description in the file) on desktop

end tell




Posted on Mar 16, 2021 7:59 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 16, 2021 10:42 AM

and if you want a droplet version that accepts dropped files vs. prompting the user:


on open theDroppedFiles
	repeat with eachFile in theDroppedFiles
		--  read each file's contents
		set theTextFileContents to read eachFile
		-- extract the folder name from line 1
		set theFolderName to paragraph 1 of theTextFileContents
		-- extract the location from line 2 (a UNIX path)
		set theLocation to POSIX file (paragraph 2 of theTextFileContents)
		
		-- make the directory
		tell application "Finder"
			make new folder at theLocation with properties {name:theFolderName}
		end tell
	end repeat
end open


Note that there's no validation that the files are text files, let alone contain the data you want. The script would need additional error checking to be considered viable.

5 replies
Question marked as Top-ranking reply

Mar 16, 2021 10:42 AM in response to Camelot

and if you want a droplet version that accepts dropped files vs. prompting the user:


on open theDroppedFiles
	repeat with eachFile in theDroppedFiles
		--  read each file's contents
		set theTextFileContents to read eachFile
		-- extract the folder name from line 1
		set theFolderName to paragraph 1 of theTextFileContents
		-- extract the location from line 2 (a UNIX path)
		set theLocation to POSIX file (paragraph 2 of theTextFileContents)
		
		-- make the directory
		tell application "Finder"
			make new folder at theLocation with properties {name:theFolderName}
		end tell
	end repeat
end open


Note that there's no validation that the files are text files, let alone contain the data you want. The script would need additional error checking to be considered viable.

Mar 16, 2021 10:38 AM in response to Fist2360

Yes. It's trivial to have the Finder create a folder via AppleScript.


The issue is only where it gets the information as to folder name and where to create it.


If this is coming from a file (txt or xml), you just need to read the file and parse the data. Reading a basic text file will be easier than XML, but XML may hold additional data that may be useful and less error-prone.


So, decide where the data is coming from, the rest is easy.


As an example, this script reads a text file and assumes the folder name is on the first line and the location is on the second line (note: no error checking, so the code may fail if the file doesn't match this format, hence the possible advantage to XML):


-- prompt the user for the file and read its contents
set theTextFileContents to read (choose file with prompt "Select the input file")
-- extract the folder name from line 1
set theFolderName to paragraph 1 of theTextFileContents
-- extract the location from line 2 (a UNIX path)
set theLocation to POSIX file (paragraph 2 of theTextFileContents)

-- make the directory
tell application "Finder"
	make new folder at theLocation with properties {name:theFolderName}
end tell



Mar 16, 2021 1:26 PM in response to Fist2360

One could structure an XML file (let's call it test.xml) like so… replacing the username placeholder with a true username…


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<Main title="Foldernames">
    <type>folder</type>
    <name>barfoo</name>
    <path>/Users/username/Desktop/</path>
</Main>



and the following AppleScript would create a new folder as barfoo at the path location:


(*
	Given an XML file like the following make a new folder at path with the given name
	
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<Main title="Foldernames">
    <type>folder</type>
    <name>barfoo</name>
    <path>/Users/username/Desktop/</path>
</Main>
*)

set XMLfile to (path to desktop as text) & "test.xml"
tell application "System Events"
	tell XML element 1 of contents of XML file XMLfile
		set typeTxt to (value of XML element "type")
		set nameTxt to (value of XML element "name")
		set pathTxt to (value of XML element "path")
	end tell
end tell
tell application "Finder"
	set dest to POSIX file (pathTxt as text) as alias
	if typeTxt contains "folder" and not (exists folder ((dest & nameTxt) as text)) = true then
		make new folder at dest with properties {name:nameTxt}
	end if
end tell



create folder using XML

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.