Duplicating file gives strange path if on an external volume

Hi

I have a weird issue. I have a simple bit of script as follows:

tell application "Finder"
	set combined_file to (duplicate combined_file) as alias
set name of combined_file to newname & ".pdf"
end tell

When combined_file is a document in a folder under my user account, I get something like this:

All is well. The rest of my script runs smooth.

But when I do the exact same script on a document on a USB key (DATA_STICK), I get this:

This then borks the rest of the script as the alias is expressed in the wrong format.

The set name command seems to work differently depending on whether you are working on an external volume or not.

Does anyone have any explanation/solution to my problem?

Thanks

Hi kiwilegal,

there are two kind of paths i use for scripting paths in Applescript:
HFS paths

path:to:Desktop:

POSIX paths

/path/to/Desktop/

convert hfs paths to posix paths:

set my_path to POSIX path of (path to desktop folder)

convert posix paths to hfs paths:

set my_path to POSIX file "/Users/kiwilegal/Desktop/"

i suggest you to put quotes at the beginning and end of posix paths, like this:

set my_path to "'/Users/kiwilegal/Desktop/" &"folder A/'"

.so that your posix path considers white spaces too. Hope this clarifies your understanding about paths.

Thanks.

I guess i am still wondering why the result for combined_file gives an POSIX result when the targeted file is on a volume, but a HFS result when it is under the user’s home account.

I will have a play around to try and force to HFS.

Cheers

Try

set combined_file to (duplicate combined_file)
combined_file as alias

I can’t reproduce the problem with a FAT32 pen.

Hello!

It sure is some other filesystem on that pen then, so that hfspaths won’t be created, and returns a kind of hybrid solution.

It is interesting to know.

The safe thing would be to coerce the alias to text first, and take it from there.


if fn contains ":" then 
# .

else 
# fn contains "/" 
end if 

kiwilegal,

The answer is because Mac os is organized in a certain folder order. :slight_smile:
The entire file system is build with nested folders. You need to know where a folder is stored in, to get its contents. Consider to work with alternatives, like posix paths too.

set f to ((path to startup disk) & "Volumes:" as text)
choose file default location alias f

you’ll get a simple description of hfs paths here:
https://secure.wikimedia.org/wikipedia/en/wiki/HFS_Plus
and posix paths here:
https://secure.wikimedia.org/wikipedia/en/wiki/Path_(computing)

What file system is on the USB drive?
Does this syntax work?


tell application "Finder"
	set duplicated_file to (duplicate combined_file) as text
	set name of file duplicated_file to newname & ".pdf"
end tell

Thanks for the replies. Sorry about delayed reply. Have been travelling. USB key is windows format. I amnot near my mac for a few days so will not be able to test until next week.

I will be sure to report back

Cheers

Hi

Just reverting on this. Have made progress but do not yet have answer. Stefan, your syntax did not work for me. Mouramartin’s did, but has not produced a solution yet.

set combined_file to (duplicate combined_file)
combined_file as alias

This script duplicates fine.

But when I rename the combined file, the new name is not reflected in combined_file. Combined_file retains the original duplicated file name.

Thus:

set combined_file to alias "DATA_STICK:untitled folder:Notes Tdshniques 14.14.52.pdf"

tell application "Finder"
	set combined_file to (duplicate combined_file)
	combined_file as alias
	set name of combined_file to "ok" & ".pdf"
end tell

…duplicates the targeted file and renames it correctly “ok.pdf”, but the value returned for combined_file reflects the original name and not “ok.pdf”. So, again, rest of my script borks as combined_file does not point to “ok.pdf”

BYW, this is a FAT32 issue as my original script worked fine on my internal disk, just the USB keys in FAT32 giving problems.

Can anyone explain why when I rename the file as above, it is renamed (appears in Finder as “ok.pdf”) but combined_file does not reflect this change.

Cheers

Hi,

have you tried the syntax in post #7 ?
It avoids intentionally alias specifiers.

btw: the line


combined_file as alias

does actually nothing for the subsequent code.
It coerces the variable to alias but does not preserve the alias

Hello

I feel that I have the explanation of what you described

Trying to reproduce it, I ran this script :


# Part #1
tell application "Finder"
	activate
	select disk "TRANSCEND"
end tell # Finder

tell application "System Events" to tell application process "Finder"
	keystroke "i" using {command down}
	tell window 1
		title
		--> "Infos sur TRANSCEND"
		class of UI elements
		--> {button, button, button, scroll area, image, static text}
		value of static text 12 of first scroll area
		--> "Format :"
		value of static text 13 of first scroll area
		--> "MS-DOS (FAT16)"
	end tell # window 1
end tell # System Events & process

# Part #2
set combined_file to "TRANSCEND:dossier sans titre:abo.pdf" as alias
set newName to do shell script "date +%Y%m%d_%H%M%S.pdf"
--> "20121013_165855.pdf"
tell application "Finder"
	(container of combined_file) as text
	--> "TRANSCEND:dossier sans titre:"
	set newPath to result & newName
	log newPath
	(*TRANSCEND:dossier sans titre:20121013_165855.pdf*)
	set new_file to duplicate combined_file
	--> document file "abo copie.pdf" of folder "dossier sans titre" of disk "TRANSCEND"
	--> error number 0
	set new_file to new_file as alias
	# with a script which never called System Events return a HFS path
	--> alias "TRANSCEND:dossier sans titre:abo copie.pdf"
	# with a script which ran at least once System Events return a Unix like path
	--> alias "/Volumes/TRANSCEND/dossier sans titre/abo copie.pdf"
	set name of new_file to newName
	--> "20121013_164938.pdf"
	log combined_file
	(*alias TRANSCEND:dossier sans titre:abo.pdf*)
	log new_file
	# with a script which never called System Events return a HFS path
	(*alias TRANSCEND:dossier sans titre:abo copie.pdf*)
	# with a script which ran at least once System Events return a Unix like path
	(*alias /Volumes/TRANSCEND/dossier sans titre/abo copie.pdf*)
	set new_file to newPath as alias
	log new_file
	(*alias TRANSCEND:dossier sans titre:20121013_165855.pdf*)
end tell

# Part #3
set newName to do shell script "date +%Y%m%d_%H%M%S.pdf"
--> "20121013_165901.pdf"
tell application "System Events"
	path of container of combined_file
	--> "TRANSCEND:dossier sans titre:"
end tell # System Events
set newPath to result & newName
do shell script "cp " & quoted form of POSIX path of combined_file & space & quoted form of POSIX path of newPath
set new_file to newPath as alias
--> alias "TRANSCEND:dossier sans titre:20121013_165901.pdf"

# Part #4
set combined_file to (path to desktop as text) & "Install Logs:dossier sans titre:abo.pdf" as alias
set newName to do shell script "date +%Y%m%d_%H%M%S.pdf"
--> "20121013_165906.pdf"
tell application "Finder"
	(container of combined_file) as text
	--> "Macintosh HD:Users:myAccount:Desktop:Install Logs:dossier sans titre:"
	set newPath to result & newName
	log newPath
	(*Macintosh HD:Users:yvankoenig:Desktop:Install Logs:dossier sans titre:20121013_165906.pdf*)
	set new_file to duplicate combined_file
	--> document file "abo copie.pdf" of folder "dossier sans titre" of folder "Install Logs" of folder "Desktop" of folder "yvankoenig" of folder "Users" of startup disk
	--> error number 0
	set name of new_file to newName
	--> "20121013_165906.pdf"
	log combined_file
	(*alias Macintosh HD:Users:myAccount:Desktop:Install Logs:dossier sans titre:abo.pdf*)
	log new_file
	(*document file abo copie.pdf of folder dossier sans titre of folder Install Logs of folder Desktop of folder myAccount of folder Users of startup disk*)
	set new_file to newPath as alias
	-->alias "Macintosh HD:Users:yvankoenig:Desktop:Install Logs:dossier sans titre:20121013_165906.pdf"
end tell

The 1st part grab the format applied to my memory stick, here “MS-DOS (FAT16)”.

The 2nd part achieve what you described upon a file is stored on the memory stick.
You’re right, renaming the copy doesn’t change the pathname stored in variable pointing to.
As I wrote in the comments, the script behaved differently if it never triggered System Events or if it did.

The 3rd part achieve what you describe without triggering the infamous Finder.
You will see that with this scheme There is not the puzzling → error number 0 returned by the Finder after duplicate.

The 4th part achieve what you described to a file stored on the internal HD.
In this case, the fact that System Events was triggered or wasn’t changes nothing.

Yvan KOENIG (VALLAURIS, France) samedi 13 octobre 2012 17:21:56

Thanks for the above comments. The code in post 7 did not work any differently to a straight duplication command, Stefan.

What I did in the end was use Yvan’s call to System Events to get the path, and force the name change into combined_file:


tell application "Finder
-----> Rest of script
tell application "System Events"
				set filepath to path of container of combined_file as text
			end tell
set combined_file to (duplicate combined_file) as alias
set name of combined_file to newname & ".pdf"
			set combined_file to filepath & newname & ".pdf" as alias
----->
end tell

Combined_file then works as expected for the rest of the script whether the targeted file in on internal drive or on FAT32 volume.

Many thanks