Why do all Scripts I make retain last: set path & *.zip ???

Hi All,
I’ve made a Script to Unzip a file, then Copy the kind “font” to the Fonts Folder. I did this once with one font. Great.

But when I make another Script or Save As App to Unzip a different ZIP file of fonts, AppleScript always remembers the very first coding to unzip just that first font. I’m Pasting the code into a New Script then changing the ‘path_2’ and ‘xxx.zip’. Could that be the issue? Should I be always actually creating a the code from scratch each time? Anyone had this experience?

~ ~ ~

tell application "Finder"
	set path_2 to quoted form of POSIX path of (folder of (path to me) as alias)
end tell
tell application "System Events"
	do shell script "unzip -u " & path_2 & "4Fonts.zip" & " -d " & path_2
end tell

set fFont to alias ((path to desktop as Unicode text) & "Fonts")

tell application "Finder"
	set theFonts to (get files of entire contents of fFont whose kind contains "font")
	set nameFonts to {}
	repeat with i in theFonts
		set end of nameFonts to name of contents of i
	end repeat
	do shell script "cp -f " & quoted form of POSIX path of fFont & "* /Library/Fonts/" -- use shell script to avoid authentication
end tell

~ ~ ~

Model: MacPro
AppleScript: 2.3 (118)
Browser: Firefox 6.0.2
Operating System: Mac OS X (10.6)

What you are describing doesn’t make sense. The only time anything is remembered between launches of a script are “properties”. I don’t see any of your variables defined as properties so I don’t have a suggestion for the behavior. There is two issues with your script though.

  1. Your “do shell script” lines are in application tell blocks, first a system events one and then a Finder block. They are applescript commands and should be taken out of any “tell application” blocks of code. Maybe you have something happening because of this???

  2. Your “repeat with i in theFonts” code doesn’t seem to do anything.

Globals and run-handler variables are also remembered between runs. But this is a mystery, since ‘path_2’ is eplicitly reset at the top of the script ” and simply pasting the code into a fresh script shouldn’t cause any ‘remembering’ effect. :confused:

I agree with all of that, so I’ve simplified the code, but Run still executes the first ‘path_2’ and the '.zip file that was unzipped in my initial test. I’ve tried entering the code from scratch several times in a New script. Created it and the zip file in a new, separate Folder, all to no avail.

~ ~ ~
tell application “Finder”
set path_2 to quoted form of POSIX path of (folder of (path to me) as alias)
end tell

do shell script "unzip -u " & path_2 & “4Fonts.zip” & " -d " & path_2

set fFont to alias ((path to desktop as Unicode text) & “Fonts”)

do shell script "cp -f " & quoted form of POSIX path of fFont & “* /Library/Fonts/”
~ ~ ~


Model: MacPro
AppleScript: 2.3 (118)
Browser: Firefox 6.0.2
Operating System: Mac OS X (10.6)

I see a problem in your code now. In the Finder code you get the quoted form of the value. Then in the first “do shell script” line you try adding a string to that quoted value. That won’t work because the folder path is quoted. Then again later you do the same, so I added parenthesis around stuff to make it calculate the paths correctly before quoting. You also don’t need “alias” in the fFont line but I did add a colon after the folder name to indicate it’s a folder. Try this…

tell application "Finder"
	set path_2 to POSIX path of (folder of (path to me) as alias)
end tell

do shell script "unzip -u " & quoted form of (path_2 & "4Fonts.zip") & " -d " & quoted form of path_2

set fFont to POSIX path of ((path to desktop as Unicode text) & "Fonts:")

do shell script "cp -f " & quoted form of (fFont & "* /Library/Fonts/")

Tried that and received:

error “usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file … target_directory” number 64


Your last line probably should be this. But you know what the paths should be so work on them. I’m just guessing and pointing out that you were performing the “quoted form of” stuff wrong.

do shell script "cp -f " & quoted form of (fFont & "*") & " /Library/Fonts/"

Well the first thig to say is that f you have saved your script as a stay open script, then the script never quits until you shut down the computer, so it may be keeping the values of your variables for that reason, so check that you have not saved as a stay open app.

Secondly as Regulus6633 has suggested, try moving your qutoted form of statements into the do shell script command, instead of the set Path_2 variable line.
Also don’t use alias types when using shell script commands, try sticking to text types.

I have re wrote a script that does what your script does, but only uses one shell script command, whenever possible dont use shell scripts as they only need one wrong bit of typing to cause problems.
I have not used the unzip unix utility, so I have to trust that you have typed it in correctly.
Instead I have used Finder to move the font files to the /Library/Fonts, if you have a tell Finder block you may as well get Finder to do as much as it can for you.

[code]on run
tell application “Finder”
set zip_folder to POSIX path of (folder of (path to me) as text) as text
set zip_file to zip_folder & “4Fonts.zip” as text
my unzip_file(zip_file, zip_folder)
delay 0.5
set desktop_fonts_folder to (path to desktop as text) & “Fonts” as alias
set library_fonts_folder to (path to library folder as text) & “Fonts” as alias
move (every file of folder desktop_fonts_folder whose kind contains ¬
“font”) to library_fonts_folder with replacing
end tell
end run

on unzip_file(zip_file, zip_folder)
do shell script "unzip -u " & quoted form of zip_file & " -d " & quoted form of zip_folder
on error error_message number error_number
display alert error_message & space & error_number message ¬
“An error occured while trying to unzip the zip file,” & return & ¬
“check that the do shell script command syntax” & return & ¬
“has been typed correctly.” as warning
end try
end unzip_file[/code]
Notice that I have moved the do shell script command out of the tell block, and put it in its own handler, and also added some error checking into that handler, to catch any problems in the shell script command.
I have also added a half second delay after the unzip_file handler call, this is always a good idea with shell script commands, as they take some time to complete, and you dont want the script goining to the next line of code, until the shell script command is fully completed, you can remove this delay command line if it works fine without it, or increase the delay time if errors occur.

Remeber that if you want to point at a different zip file, then change the “4Fonts.zip” name assigned to the zip_file variable to the new zip file name, and also the zip_folder variable will have to be changed if the new zip file resides in a different folder.

You might want to consider adding a on open handler to your script, that way you could drag and drop a zip file onto your compiled script, and have it run the code on the dropped file.

Try these suggestions and let me know how you get on.

Regards Mark

I suspect it’s just you. Being able to do lots of new things has put some of the fun back into AppleScript, I reckon.

Yeah your probably right Shane, it might just be me.

Dont get me wrong, I think ApplescriptObjC is a great tool for Applescripter’s, and I have had some fun learning it.
But as a hobbyist programmer that does it for fun, it just sometimes feel more like hard work, I think this is because
as a learner, you spend more time reading Xcode documentation, than you do writing code.

I will be continuing to learn more about AOC, and I hope that at some time in the future, I will enjoy it more, this will probably happen as I get more compatant at using it.

For IT pro’s like yourself, I’m sure its the tool you have been waiting for, and yes I have learnt a lot from your posts on this forum, so thanks for that, and keep up the good work.

Regards Mark