Understand & help with Get Folder Contents script

I have this script, possibly written by Nigel Garvey, that gets file names from a folder and writes them to a text file. I have used this script for years, and thanks again.

I am trying to modify it so I can write to clipboard. Which I have done.

I would like to make 2 changes to the script and I cannot figure out how to do them.

The first issue is to remove the “Header” line that is added to the file / clipboard. See second commented line, “Just the heading”.

The second issue is to remove the file extensions. I have tried changing all the delimiters. I am not sure where a hide extensions request would go, in the createText function or main?


-- Create the text to be written to the file.
-- Just a heading and the item names, indented according to their positions in the hierarchy.
-- (Uncomment the (* *) comment markers to preserve the full paths.)
on createText(posixPaths)
	script o
		property paths : posixPaths
	end script
	
	set rootPath to beginning of o's paths
	set item 1 of o's paths to "Contents of " & rootPath & linefeed
	set astid to AppleScript's text item delimiters
	-- (*
	considering case
		set AppleScript's text item delimiters to ""
		set tabStr to {tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab} as text -- Hopefully more than needed!
		
		set AppleScript's text item delimiters to "/"
		set nonIndent to (count rootPath's text items) -- 1
		
		set len to (count posixPaths)
		repeat with i from 2 to len
			set thisPath to item i of o's paths
			set tiCount to (count thisPath's text items)
			set thisName to text item -1 of thisPath
			-- If the item name contains any colons, restore the original slashes.
			if (thisName contains ":") then
				set AppleScript's text item delimiters to ":"
				set thisName to thisName's text items
				set AppleScript's text item delimiters to "/"
				set thisName to thisName as text
			end if
			-- If this is a folder path, append a colon to the name.
			if ((i < len) and (item (i + 1) of o's paths begins with thisPath)) then set thisName to thisName & ":" -- or "/", if preferred.
			set item i of o's paths to text 1 thru (tiCount - nonIndent) of tabStr & thisName
		end repeat
		-- *)
		set AppleScript's text item delimiters to linefeed
		set outText to o's paths as text
		-- (*
		set AppleScript's text item delimiters to linefeed & tab
		set outText to outText's text items
		set AppleScript's text item delimiters to linefeed
		set outText to outText as text
		
	end considering
	-- *)
	set AppleScript's text item delimiters to astid -- Set Delimiters Back !!!
	
	return outText
end createText

on main()
	set rootFolder to (choose folder with prompt "Choose a folder to catalogue.")
	
	-- List the hierarchy as POSIX paths, omitting any that contain elements beginning with ".".
	set thePaths to paragraphs of (do shell script "find -f " & (quoted form of POSIX path of rootFolder) & " \\! -path \"*/.*\"")
	
	set outText to createText(thePaths)
	set the clipboard to outText
end main

main()

Thanks,
Randy

Yeah. That’s looks like one of mine.

The easiest way to patch for that (without actually rewriting the script not to generate the header in the first place, of course) is to have the createText() handler output the text starting at the third paragraph.

In createText(). It just needs another ‘if’ section in the repeat to remove the extension, if any, from each name.

-- Modified version which suppresses the output of the header and any extensions in the names.

-- Create the text to be written to the file.
-- Just a heading and the item names, indented according to their positions in the hierarchy.
-- (Uncomment the (* *) comment markers to preserve the full paths.)
on createText(posixPaths)
	script o
		property paths : posixPaths
	end script
	
	set rootPath to beginning of o's paths
	set item 1 of o's paths to "Contents of " & rootPath & linefeed
	set astid to AppleScript's text item delimiters
	-- (*
	considering case
		set AppleScript's text item delimiters to ""
		set tabStr to {tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab} as text -- Hopefully more than needed!
		
		set AppleScript's text item delimiters to "/"
		set nonIndent to (count rootPath's text items) -- 1
		
		set len to (count posixPaths)
		repeat with i from 2 to len
			set thisPath to item i of o's paths
			set tiCount to (count thisPath's text items)
			set thisName to text item -1 of thisPath
			if (thisName contains ".") then -- New section to remove any extension.
				set AppleScript's text item delimiters to "."
				set thisName to text 1 thru text item -2 of thisName
				set AppleScript's text item delimiters to "/"
			end if
			-- If the item name contains any colons, restore the original slashes.
			if (thisName contains ":") then
				set AppleScript's text item delimiters to ":"
				set thisName to thisName's text items
				set AppleScript's text item delimiters to "/"
				set thisName to thisName as text
			end if
			-- If this is a folder path, append a colon to the name.
			if ((i < len) and (item (i + 1) of o's paths begins with thisPath)) then set thisName to thisName & ":" -- or "/", if preferred.
			set item i of o's paths to text 1 thru (tiCount - nonIndent) of tabStr & thisName
		end repeat
		-- *)
		set AppleScript's text item delimiters to linefeed
		set outText to o's paths as text
		-- (*
		set AppleScript's text item delimiters to linefeed & tab
		set outText to outText's text items
		set AppleScript's text item delimiters to linefeed
		set outText to outText as text
		
	end considering
	-- *)
	set AppleScript's text item delimiters to astid -- Set Delimiters Back !!!
	
	return text from paragraph 3 to -1 of outText -- Return all but the first two paragraphs of the generated text.
end createText

on main()
	set rootFolder to (choose folder with prompt "Choose a folder to catalogue.")
	
	-- List the hierarchy as POSIX paths, omitting any that contain elements beginning with ".".
	set thePaths to paragraphs of (do shell script "find -f " & (quoted form of POSIX path of rootFolder) & " \\! -path \"*/.*\"")
	
	set outText to createText(thePaths)
	set the clipboard to outText
end main

main()

Thanks again Nigel,

I was just going through Sal’s Applescript 1-2-3 book and he was mentioning using negative numbers to get to end of a list for example.

Well commented, this helps me learn. Just like your original with the comments in the header, that helped me understand parts of the script.

Cheers, Randy!