Lower case

Hello,

I use the following applescript to copy files whose file name is not contained in a txt file.
It works fine, except that my file extensions are a mix of upper and lower case: (*.psd and .PSD)
In the txt file all filenames are lowercase (
.psd)
How/where do I put lowercase on my filenames before checking against the txt file?

set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
tell application "Finder"
	set theFiles to a reference to entire contents of folder files_source
	repeat with thisItem in theFiles
		set theFileName to name of thisItem as text
		if theFileName is not in files_list then
			try
				-- or move thisItem to files_dest if you want to move it
				duplicate thisItem to files_dest
			on error
				display dialog "Duplicate files named " & theFileName & ". Overwrite or skip?" buttons {"Cancel", "Overwrite", "Skip"} with icon caution
				if button returned of the result is "Overwrite" then
					duplicate thisItem to files_dest with replacing
				end if
			end try
		end if
	end repeat
end tell

set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
	try
		do shell script "cp " & quoted form of POSIX path of files_source & quoted form of aParagraph & space & quoted form of POSIX path of files_dest
	end try
end repeat

From my point of view, some instructions are wrongly written.

(1) set files_source to (choose folder with prompt “Choose the source folder”) as text
What need to coerce the alias item returned by choose folder into text to call it later as a folder ?
Keet the alias as is and speak to it directly below.

(2) set files_dest to (choose folder with prompt “Choose your destination folder for the copied files.”) as alias
What need to coerce into alias an item which is already an alias ?

set file_ref to (choose file with prompt “Select a txt file to read:” default location files_dest)

(3) set files_list to paragraphs of (read file_ref as «class utf8»)
I don’t know your text file so maybe using «class utf8» is right for it, here it was useless.

tell application “Finder”
(4) set theFiles to a reference to entire contents of folder files_source
According to what I wrote above (1), if you leave files_source of class alias, here you may code :
set theFiles to a reference to entire contents of files_source
repeat with thisItem in theFiles
set theFileName to name of thisItem as text
if theFileName is not in files_list then
try
– or move thisItem to files_dest if you want to move it
duplicate thisItem to files_dest
on error
display dialog "Duplicate files named " & theFileName & “. Overwrite or skip?” buttons {“Cancel”, “Overwrite”, “Skip”} with icon caution
if button returned of the result is “Overwrite” then
duplicate thisItem to files_dest with replacing
end if
end try
end if
end repeat
end tell

This said, this first part of your script worked flawlessly here.

Now it’s time to speak of the second part.

set files_source to (choose folder with prompt “Choose the source folder”) as text
set files_dest to (choose folder with prompt “Choose your destination folder for the copied files.”) as alias
set file_ref to (choose file with prompt “Select a txt file to read:” default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
try

(5) do shell script "cp " & quoted form of POSIX path of files_source & quoted form of aParagraph & space & quoted form of POSIX path of files_dest
I’m not sure that it’s wrong but I don’t like to concatenate quoted strings. I think that it is neater to quote after concatenation.
do shell script "cp " & quoted form of( POSIX path of files_source & aParagraph) & space & quoted form of POSIX path of files_dest
end try
end repeat

Here, running :

set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
	try
		do shell script "cp " & quoted form of (POSIX path of files_source & aParagraph) & space & quoted form of POSIX path of files_dest
	end try
end repeat

It copied the files listed in the text file.
But I’m not sure that the text file contain only file names. Maybe it contain folder names.
In such case the code must be edited because your original instruction refuse to treat folders.

This edited version is able to copy folders(with their contents) and here it did the job.

set files_source to (choose folder with prompt "Choose the source folder") as text
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as «class utf8»)
repeat with aParagraph in files_list
	try
		do shell script "cp -R " & quoted form of (POSIX path of files_source & aParagraph) & space & quoted form of POSIX path of files_dest
	end try
end repeat

Yvan KOENIG (VALLAURIS, France) lundi 13 octobre 2014 15:01:52

Thanks Yvan,

As you wrote, it seems that the first part worked very well.
Actually there is no need to run 2-part.

It also seems that it is not necessary to make lower case on the files after all, so everything is ok her :slight_smile:

Sorry about this, but thanks for taking the time to help me.

Best Regards

Lars

Don’t worry, I was far from my mac for height days so I am not too tired.

The standard HFS plus is not case sensitive.
We may install its case sensitive cousin but doing that create problem with a lot of applications.
So, PDF, Pdf, PdF., pdf are treated as a single extension.
When I read some comments in Apple Discussions forums about changes introduced since Lion, I guess that if the operating system is edited to default to the case sensitive behavior, it will be a Revolution and, like in 1789 in France, some heads will be cut.

Yvan KOENIG (VALLAURIS, France) lundi 13 octobre 2014 17:32:39

Mostly they are, but not always. There are some routines developers can use that are, sadly, case-sensitive. IMO it’s safer to stick with lowercase if possible.

According to Shane’s comment, here is an enhanced version converting the extension to lowercase.

use theLib : script "changeCase Lib" # requires changeCase Lib.scptd in /Library/Script Libraries folder
use scripting additions # required because we use scripting addition's functions

set files_source to (choose folder with prompt "Choose the source folder") # it's an alias
set files_dest to (choose folder with prompt "Choose your destination folder for the copied files.") # it's an alias
set file_ref to (choose file with prompt "Select a txt file to read:" default location files_dest) # it's an alias
set files_list to paragraphs of (read file_ref as «class utf8»)

tell application "Finder"
	set theFiles to a reference to entire contents of folder files_source
	repeat with thisItem in theFiles
		set theFileName to name of thisItem
		if theFileName is not ".DS_Store" then # I was tired to be asked what to do with these files
			if theFileName is not in files_list then
				set theExt to name extension of thisItem as text
				if theExt > "" then
					set theExt to (theLib's makeLowerOf:theExt)
					set theFileName to text 1 thru -(1 + (count theExt)) of theFileName & theExt
					set name of thisItem to theFileName
					# Now thisItem has a lowercased extension
				end if
				try
					-- or move thisItem to files_dest if you want to move it
					duplicate thisItem to files_dest
				on error
					display dialog "Duplicate files named " & theFileName & ". Overwrite or skip?" buttons {"Cancel", "Overwrite", "Skip"} with icon caution
					if button returned of the result is "Overwrite" then
						duplicate thisItem to files_dest with replacing
					end if
				end try
			end if
		end if
	end repeat
end tell

The contents of the library package named changeCase Lib.scptd is :

#=====

# use framework "Foundation"
on makeCapsOf:aString
	return (current application's NSString's stringWithString:aString)'s uppercaseString() as text
end makeCapsOf:

on makeLowerOf:aString
	return (current application's NSString's stringWithString:aString)'s lowercaseString() as text
end makeLowerOf:

on makeCapitalizedOf:aString
	return (current application's NSString's stringWithString:aString)'s capitalizedString() as text
end makeCapitalizedOf:

#=====

Of course, it was borrowed from a Shane’s message.

Yvan KOENIG (VALLAURIS, France) mardi 14 octobre 2014 10:55:00