Applescript do Shell Script TAR weirdity (problem)

So I made a simple script to loop through some folders and tar.bz2 them. When I created it and ran it couple of times it worked fine. Then, when I decided to use it for real and leave for a few hours I come back and it’s thrown an error ever since. Maybe I just need to restart? Anyway, here the script. If I copy and paste the variable in to the terminal it runs fine, but if I use script editor it doesn’t completely name the archive and throws a Finder got an error: Tar: removing leading ‘/’ from member names. It also throws a -10004 error. :confused:


set kDirectoryToArchive to contents of (choose folder without invisibles)

tell application "Finder"
	set subFolders to every folder of kDirectoryToArchive
	repeat with eachFolder in subFolders
		set a to eachFolder as alias
		set b to POSIX path of a
		set kName to (name of eachFolder) as string
		log kName
		
		set defDel to AppleScript's text item delimiters
		if kName contains " " then
			set AppleScript's text item delimiters to " "
			set newname to text items of kName
			set AppleScript's text item delimiters to "_"
			set thisItem to (newname as string)
			set AppleScript's text item delimiters to defDel
		end if
		
		set a to "tar -jcvf /Volumes/XX/" & thisItem & ".tar.bz2 " & quoted form of b
		log a
		do shell script a
		
	end repeat
	display dialog "All donez. kthxbye :)"
end tell

Hello!

Try this:


set kDirectoryToArchive to contents of (choose folder without invisibles)

tell application "Finder"
	set subFolders to (every folder of kDirectoryToArchive) as list
	repeat with eachFolder in subFolders
		set a to eachFolder as alias
		set b to POSIX path of a
		set kName to (name of eachFolder) as string
		log kName
		
		set defDel to AppleScript's text item delimiters
		if kName contains " " then
			set AppleScript's text item delimiters to " "
			set newname to text items of kName
			set AppleScript's text item delimiters to "_"
			set thisItem to (newname as string)
			set AppleScript's text item delimiters to defDel
		else
			set thisItem to kName
		end if
		
		set a to "tar -jcvf /Volumes/XX/" & thisItem & ".tar.bz2 " & quoted form of b
		log a
		do shell script a
		
	end repeat
	display dialog "All donez. kthxbye :)"
end tell

I think you only tested it on folders that contained spaces.

Thanks, that fixes the naming but the tar script still gives a finder error, which I guess is normal but the applescript still stops executing after the first pass. I’ll put a “try” in and see if that helps. Still don’t like ignoring warning tho.

Hi,

i guess it’s a finder error.

Move the end tell after getting the folders … before repeat

Hello!

It is a privilege violation, because an osaxen is called within an application tell block that isn’t the current application. This is new behaviour from Snow Leopard onwards. (Posix Path is a Standard Addtions call.)

Edit
It was the do shell script call, and not the Posix path of call that caused the privilege violation. Posix path of works fine even within a Finder tell block

The script should work now. :slight_smile:

property L : {}
property arcNames : {}
set kDirectoryToArchive to contents of (choose folder without invisibles)
set {L, arcNames} to {{}, {}}

tell application "Finder"
	set subFolders to (every folder of kDirectoryToArchive) as list
	repeat with eachFolder in subFolders
		set end of L to eachFolder as alias
		set end of arcNames to (name of eachFolder) as string
	end repeat
end tell
set {i, fc} to {1, count arcNames}
repeat fc times
	
	set kName to item i of my arcNames
	log kName
	
	set defDel to AppleScript's text item delimiters
	if kName contains " " then
		set AppleScript's text item delimiters to " "
		set newname to text items of kName
		set AppleScript's text item delimiters to "_"
		set thisItem to (newname as string)
		set AppleScript's text item delimiters to defDel
	else
		set thisItem to kName
	end if
	set b to POSIX path of item i of L
	set a to "tar -jcvf /Volumes/XX/" & thisItem & ".tar.bz2 " & quoted form of b
	log a
	do shell script a
	set i to i + 1
end repeat
display dialog "All donez. kthxbye :)"

I was wondering how the script may do its duty if you move end tell before the repeat statement.
So I tested.



set kDirectoryToArchive to contents of (choose folder without invisibles)

tell application "Finder"
	set subFolders to (every folder of kDirectoryToArchive) as list
end tell -- Finder

repeat with eachFolder in subFolders
	set a to eachFolder as alias
	set b to POSIX path of a
	set kName to (name of eachFolder) as string
	log kName
	
	set defDel to AppleScript's text item delimiters
	if kName contains " " then
		set AppleScript's text item delimiters to " "
		set newname to text items of kName
		set AppleScript's text item delimiters to "_"
		set thisItem to (newname as string)
		set AppleScript's text item delimiters to defDel
	else
		set thisItem to kName
	end if
	
	set a to "tar -jcvf /Volumes/XX/" & thisItem & ".tar.bz2 " & quoted form of b
	log a
	# do shell script a
	
end repeat
display dialog "All donez. kthxbye :)"

The log report is :

tell application “AppleScript Editor”
choose folder without invisibles
→ alias “Macintosh HD:Users:yvankoenig:Documents:tempo:”
end tell
tell application “Finder”
get every folder of alias “Macintosh HD:Users:yvankoenig:Documents:tempo:”
→ {folder " ebay" of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk, folder " pour Essais" of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk, folder “2011-06-06” of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk}
get folder " ebay" of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk
→ alias “Macintosh HD:Users:yvankoenig:Documents:tempo: ebay:”
get name of folder " ebay" of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk
→ " ebay"
end tell
(* ebay*)
(tar -jcvf /Volumes/XX/_ebay.tar.bz2 ‘/Users/yvankoenig/Documents/tempo/ ebay/’)
tell application “Finder”
get folder " pour Essais" of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk
→ alias “Macintosh HD:Users:yvankoenig:Documents:tempo: pour Essais:”
get name of folder " pour Essais" of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk
→ " pour Essais"
end tell
(* pour Essais*)
(tar -jcvf /Volumes/XX/_pour_Essais.tar.bz2 ‘/Users/yvankoenig/Documents/tempo/ pour Essais/’)
tell application “Finder”
get folder “2011-06-06” of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk
→ alias “Macintosh HD:Users:yvankoenig:Documents:tempo:2011-06-06:”
get name of folder “2011-06-06” of folder “tempo” of folder “Documents” of folder “yvankoenig” of folder “Users” of startup disk
→ “2011-06-06”
end tell
(2011-06-06)
(tar -jcvf /Volumes/XX/2011-06-06.tar.bz2 ‘/Users/yvankoenig/Documents/tempo/2011-06-06/’)
tell application “AppleScript Editor”
display dialog “All donez. kthxbye :)”

No, you aren’t drunk.

The script is supposed to issue an end tell statement but it continue to speak to the folder for the three instructions :
set a to eachFolder as alias
set b to POSIX path of a
set kName to (name of eachFolder) as string

This surprising behavior is specific to the Finder.

As I don’t like this application, I tried to do the trick with System Events but this one is more coherent.



set kDirectoryToArchive to contents of (choose folder without invisibles)

tell application "System Events"
	set subFolders to (path of every folder of disk item (kDirectoryToArchive as text)) as list
end tell -- System Events

repeat with eachFolder in subFolders
	set a to eachFolder as text
	set b to POSIX path of a
	set kName to (name of disk item eachFolder) as string # DON'T COMPILE !!
	log kName
	
	set defDel to AppleScript's text item delimiters
	if kName contains " " then
		set AppleScript's text item delimiters to " "
		set newname to text items of kName
		set AppleScript's text item delimiters to "_"
		set thisItem to (newname as string)
		set AppleScript's text item delimiters to defDel
	else
		set thisItem to kName
	end if
	
	set a to "tar -jcvf /Volumes/XX/" & thisItem & ".tar.bz2 " & quoted form of b
	log a
	--do shell script a
	
end repeat
display dialog "All donez. kthxbye :)"

To be able to compile I edited it as :


set kDirectoryToArchive to contents of (choose folder without invisibles)

tell application "System Events"
	set subFolders to (path of every folder of disk item (kDirectoryToArchive as text)) as list
end tell -- System Events

repeat with eachFolder in subFolders
	set a to eachFolder as text
	set b to POSIX path of a
	tell application "System Events" to set kName to (name of disk item eachFolder) as string
	log kName
	
	set defDel to AppleScript's text item delimiters
	if kName contains " " then
		set AppleScript's text item delimiters to " "
		set newname to text items of kName
		set AppleScript's text item delimiters to "_"
		set thisItem to (newname as string)
		set AppleScript's text item delimiters to defDel
	else
		set thisItem to kName
	end if
	
	set a to "tar -jcvf /Volumes/XX/" & thisItem & ".tar.bz2 " & quoted form of b
	log a
	--tell me to do shell script a
	
end repeat
display dialog "All donez. kthxbye :)"

But, my own choice would be :


set kDirectoryToArchive to contents of (choose folder without invisibles)

tell application "System Events"
	set subFolders to (path of every folder of disk item (kDirectoryToArchive as text)) as list
	repeat with eachFolder in subFolders
		set kName to (name of disk item eachFolder) as string
		log kName
		my archiveAfolder(eachFolder, kName)
	end repeat
end tell
display dialog "All donez. kthxbye :)"

on archiveAfolder(a_Folder, k_Name)
	if k_Name contains " " then
		set defDel to AppleScript's text item delimiters
		set AppleScript's text item delimiters to " "
		set newname to text items of k_Name
		set AppleScript's text item delimiters to "_"
		set thisItem to (newname as string)
		set AppleScript's text item delimiters to defDel
	else
		set thisItem to k_Name
	end if
	quoted form of POSIX path of a_Folder
	set a to "tar -jcvf /Volumes/XX/" & thisItem & ".tar.bz2 " & result
	log a
	do shell script a
end archiveAfolder

Yvan KOENIG (VALLAURIS, France) dimanche 9 septembre 2012 21:48:50

Ups,

sorry, didn’t have a close look, should have seen it.
But funny that I didn’t test it… it’s been wrong… but succeeds … :wink:

Thanks for your explanations!

Hans-gerd Claßen

Salu Yvan,

There are too many useless coercions, why not


set kDirectoryToArchive to (choose folder) -- without invisibles is default

tell application "System Events" to set subFolders to every folder of kDirectoryToArchive -- every . is always list
repeat with eachFolder in subFolders
	set kName to name of eachFolder -- name is always text
	log kName
	archiveAfolder(eachFolder as text, kName)
end repeat


Bonjour Stefan

I agree with most of your changes.

I thought that System Events was unable to work upon aliases.

But there is an error in your code.

archiveAfolder(eachFolder as text, kName)

fails with the message :
error “Il est impossible de rendre «class cfol» "Macintosh HD:Users:yvankoenig:Desktop:XL & CSV Æ’:subfolder 1:" of application "System Events" en type text.” number -1700 from «class cfol» “Macintosh HD:Users:yvankoenig:Desktop:XL & CSV Æ’:subfolder 1:” to text

we must edit it as :
archiveAfolder(path of eachFolder as text, kName)

Yvan KOENIG (VALLAURIS, France) lundi 10 septembre 2012 18:34:35

Right, I should always test scripts before posting them :wink:
but then you can omit the coercion


archiveAfolder(path of eachFolder, kName) -- path is always text

In french we say :

un point partout, la balle au centre :wink:

Yvan KOENIG (VALLAURIS, France) lundi 10 septembre 2012 18:45:24

(1) In an other thread I failed to test my late script and an instruction was missing.

(2) One more time I’m puzzled.
Here, in your version of the script, the unique instruction supposed to be addressed to System Events is
set subFolders to every folder of kDirectoryToArchive – every . is always list

but in the log report,
set kName to name of eachFolder
and
path of eachFolder

appear as addressed to System Events.

tell application “System Events”
get name of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 1:”
→ “subfolder 1”
end tell
(subfolder 1)
tell application “System Events”
get path of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 1:”
→ “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 1:”
end tell
(tar -jcvf /Volumes/XX/subfolder_1.tar.bz2 ‘/Users//Desktop/XL & CSV Æ’/subfolder 1/’)
tell application “System Events”
get name of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 2:”
→ “subfolder 2”
end tell
(subfolder 2)
tell application “System Events”
get path of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 2:”
→ “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 2:”
end tell
(tar -jcvf /Volumes/XX/subfolder_2.tar.bz2 ‘/Users//Desktop/XL & CSV Æ’/subfolder 2/’)

I’m really wondering because in my long message (#6) I gave an example in which, in an instruction located out of a tell System Events . end tell block,
(set kName to (name of disk item eachFolder) as string)
I was forced to insert the statement
tell application “System Events”
to get a compiled script.

Yvan KOENIG (VALLAURIS, France) lundi 10 septembre 2012 21:41:45

In the first case you have an object with the properties name and path.
These keywords are included in AppleScript itself, so there is no need to resolve terminology.
On the other hand disk item is a special term of System Events therefore the terminology must be resolved

OK Stefan but what is puzzling for me is the fact that these script ask System Events to give the value of these properties.
I would not be surprised if the log report was :

get name of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 1:”
→ “subfolder 1”
(subfolder 1)
get path of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 1:”
→ “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 1:”
(tar -jcvf /Volumes/XX/subfolder_1.tar.bz2 ‘/Users//Desktop/XL & CSV Æ’/subfolder 1/’)
tell application “System Events”
get name of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 2:”
→ “subfolder 2”
(subfolder 2)
get path of folder “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 2:”
→ “Macintosh HD:Users::desktop:XL & CSV Æ’:subfolder 2:”
(tar -jcvf /Volumes/XX/subfolder_2.tar.bz2 ‘/Users//Desktop/XL & CSV Æ’/subfolder 2/’)

But maybe it’s my brain which is not sufficiently efficient.

Re-reading the log report, I choose to split your instruction triggering System Events.
tell application “System Events”
set subFolders to every folder of kDirectoryToArchive – every . is always list
.
end tell
.

Doing that, the number of statements tell application “System Events”
is divided by quite 2 in the log report.

Yvan KOENIG (VALLAURIS, France) lundi 10 septembre 2012 22:10:20

the script asks System Events because the object belongs to System Events.
Consider that the application tell blocks are only necessary at compile time to resolve the terminology.
At runtime AppleScript knows who to ask

Yikes! I go away for a day and I see I have a lot to go through on this thread. :lol:
Thanks for all the contributions to this.

Just for the record: It’s do shell script that is causing the privilege violation.

set kDirectoryToArchive to (choose folder)

tell application "System Events" to set subFolders to every folder of kDirectoryToArchive
repeat with eachFolder in subFolders
	do shell script "tar -jcvf /Volumes/XX/$(echo " & quoted form of (name of eachFolder as text) & " | tr '[ /]' '[_:]').tar.bz2 " & quoted form of POSIX path of (path of eachFolder as text)
end repeat


EDIT:
The script now change the “/” in the name to “:” for file names containing a “/” (they are actually a “:”)

That is correct, I’ll jump up and correct that post. (I tend to avoid using do shell script calls within tell blocks, so I didn’t see that one. Thanks! :slight_smile:

By the way: POSIX path is not a Standard Addtions call (contrary to POSIX file)

And it isn’t a command verb, it is a property.

What can I say? I have to read everything in the Standard Addtions Dictonary. It is listed in that in Script Debugger. 4.5. :slight_smile:

Thanks, now I am aware of it, I’ll read the dictionary more carefully for the future.