Can figure out this simple AppleScript

This little script is designed to save my Minecraft savegames. It basically zips up the saves dir and retitles it with the date (and the OS will save the timestamp).

It’s not working though - it’s spitting out a permission denied I/O error. Any help?

Thanks!


#!/bin/sh	
display dialog "Do you want to backup your world(s) first?" buttons {"No, just play", "Yes, please!"}
if result = {button returned:"No, just play"} then
	tell application "Minecraft" to activate
else if result = {button returned:"Yes, please"} then
end if
tell application "Finder"
	set theItem to POSIX path of "Users:Hermes:Library:Application Support:minecraft:"
	set itemPath to quoted form of theItem & "saves/"
	set timeStamp to do shell script "/bin/date +%d-%m-%Y"
	set zipFile to quoted form of (timeStamp & ".zip")
	do shell script "zip -r " & zipFile & " " & itemPath
end tell

You’re trying to build a POSIX path from an HFS path, but the HFS path is malformed – it must include the volume name. Use something like this instead:

set newPath to (path to application support as text) & "minecraft"
set theItem to POSIX path of newPath

And get rid of the tell application “Finder” – you’re not scripting the Finder at all.

Thanks for your reply, but unfortunately it’s still giving me the same error. Dang

Have you explored inside the /Library/Application Support/Minecraft/ folder? There may a folder in there that you can utilize.

Additionally, try looking in your ~/Library/Application Support/ folderl, there may be locations that are usable there.

You possibly want this:

#!/bin/sh	
display dialog "Do you want to backup your world(s) first?" buttons {"No, just play", "Yes, please!"}
if result = {button returned:"No, just play"} then
	--	tell application "Minecraft" to activate
else if result = {button returned:"Yes, please"} then
end if
set newPath to (path to application support as text) & "minecraft:saves"
set theItem to quoted form of POSIX path of newPath
set timeStamp to do shell script "/bin/date +%d-%m-%Y"
set zipFile to quoted form of ((path to desktop as text) & timeStamp & ".zip")
do shell script "zip -r " & zipFile & " " & theItem

But there may be permissions issues.

the default syntax path to application support points to the local domain so you have to add the user domain

set theItem to POSIX path of (path to application support from user domain) & "minecraft/saves"

and the destination file must be a POSIX path, too

set zipFile to quoted form of (POSIX path of (path to desktop) & timeStamp & ".zip")

Probably a long shot, but have you made it executable?

AppleScripts are executable by default

StefanK that solved the problem. Thank you!

Two more questions, to finish this little one.

  1. How can I have it so the zip will just save the “saves” folder and not the entire directory path?
  2. How can I have the script delete the oldest of X amount of zips, aka backups, so they don’t stockpile?

Thank you all! :slight_smile:

Edit: I’ve tried to add a couple more lines so it will create a folder on the Desktop if one doesn’t already exist. I may have broken it again (note about where code will go isn’t in compiled version):


#!/bin/sh    
display dialog "Do you want to backup your world(s) first?" buttons {"No, just play", "Yes, please!"}
if result = {button returned:"No, just play"} then
	--    tell application "Minecraft" to activate
else if result = {button returned:"Yes, please"} then
	set newPath to POSIX path of (path to application support from user domain) & "minecraft/saves"
	set theItem to quoted form of POSIX path of newPath
	set timeStamp to do shell script "/bin/date +%d-%m-%Y_%H%M"
	set outputFolder to (POSIX path of (path to desktop))
	set backupFolder to "Minecraft Backups"
	set zipFile to quoted form of outputFolder & timeStamp & ".zip"
	tell application "Finder"
		if not (exists folder backupFolder in folder outputFolder) then
			make new folder at outputFolder with properties {name:backupFolder}
		else
			set outputFolder to (POSIX path of (path to desktop)) & "Minecraft Backups"
		end if
	end tell
	do shell script "zip -r " & zipFile & " " & theItem
	display dialog "Deleting oldest backup..."
[i]	****** CODE TO DELETE OLD ZIPS IN "MINECRAFT BACKUPS GOES HERE" *****[/i]
	display dialog "All done... now you can die without consequence"
	tell application "Minecraft" to activate
end if

Can you tell I’m a complete n00b?

your main issue is the missing exclamation point after button returned:“Yes, please” .

To create a zip archive including files and folders, strip the folder hierarchy above the current directory and create intermediate destination directories automatically I recommend to use the shell command ditto

The script deletes the oldest backup if there are more than one


set {button returned:buttonReturned} to display dialog "Do you want to backup your world(s) first?" buttons {"No, just play", "Yes, please!"}
if buttonReturned starts with "No" then
	-- tell application "Minecraft" to activate
else if buttonReturned starts with "Yes" then
	set theItem to POSIX path of (path to application support from user domain) & "minecraft/saves"
	set timeStamp to do shell script "/bin/date +%d-%m-%Y_%H%M"
	set outputFolder to POSIX path of (path to desktop) & "Minecraft Backups/"
	set zipFile to quoted form of outputFolder & timeStamp & ".zip"
	do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of theItem & space & zipFile
	
	display dialog "Deleting oldest backup..." giving up after 1
	tell application "Finder"
		set backupFiles to sort (get files of folder "Minecraft Backups" whose name extension is "zip") by creation date
		if (count backupFiles) > 1 then delete item 1 of backupFiles
	end tell
	display dialog "All done... now you can die without consequence"
	tell application "Minecraft" to activate
end if


PS: for AppleScripts the shell definition #!/bin/sh is not needed “ AppleScript treats a leading number sign as comment anyway

Thanks Stefan.

It’s all done. :slight_smile:

Hi Stefan,

Actually I thought it might be a unix script for some reason.

Have a good day,
kel