?

Log in

Finder renaming script? - applescript [entries|archive|friends|userinfo]
applescript

[ website | Apple's AppleScript Home Page ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Finder renaming script? [Jun. 17th, 2011|01:36 pm]
applescript

applescript

[johncoxon]
Hey all.

Before I tell you the script I'm trying to use I want to give you guys an idea of what I'm trying to achieve with it. I'm an avid Minecraft player, and the game places a folder in Application Support called Minecraft. In this folder is a file: /bin/minecraft.jar. In order to mod the game, you rename the file to minecraft-original.jar, unzip and then put new files in the resulting minecraft.jar folder. In order to bring back your vanilla Minecraft installation, you rename the folder minecraft.jar to minecraft-mods.jar, and the file called minecraft-original.jar is renamed minecraft.jar.

What I am attempting to do is create an AppleScript that looks to see whether the folder minecraft-mods.jar exists, and if it does, go back to the vanilla build, but if it doesn't, go to the modded build. My proposed script is below:

tell application "Finder"
set thisFolder to "/Users/John/Dropbox/Minecraft/minecraft/bin"
if folder "minecraft-mods.jar" of thisFolder exists then
set file "minecraft.jar" of thisFolder as "minecraft-original.jar"
set folder "minecraft-mods.jar" of thisFolder as "minecraft.jar"
else
set folder "minecraft.jar" of thisFolder as "minecraft-mods.jar"
set file "minecraft-original.jar" of thisFolder as "minecraft.jar"
end if
end tell

I get the error, "Expected class name but found """." What am I doing wrong?
link

Comments:
[User Picture]From: kiodane
2011-06-17 03:56 pm (UTC)
I use bash to script my Minecraft server. I still have to update
  plugins by hand, but that hasn't become too much of a problem in
  recent Bukkit releases.
- Here's my launch script
    #!/bin/bash


      cd ~/Minecraft/Server/  #change this to the directory your
      server files are in

      mkdir -p ./logs/  #creates a log folder so old logs aren't lost
      when the server restarts
      fileDate=`date "+%Y-%m-%d--%H-%M-%S"`      #get date and time
      mv -f ./server.log ./logs/MC-server-$fileDate.log    #move old
      log to logs folder

      #And finally, start the server

      #screen -S minecraft java -Xmx1024M -Xms1024M -jar
      minecraft_server.jar nogui #unmodded

      screen -S minecraft java -Xms512M -Xmx1024M -jar craftbukkit.jar
      #bukkit



- Here's my Bukkit update script
    #!/bin/bash

      screen -S minecraft -X stuff "
      say [Auto Message] Server Stopping
      stop
      "

      #do the updates

      cd ~/Minecraft/Server/

      mv -f craftbukkit.jar craftbukkit.jar.old
      curl -o craftbukkit.jar
      http://ci.bukkit.org/job/dev-CraftBukkit/Recommended/artifact/targ
      et/craftbukkit-0.0.1-SNAPSHOT.jar

      echo
      echo "Server updated"

      #reboot the server

      ~/Minecraft/Server/bashscripts/launchMCServer.command

I have more scripts for generating maps, export the world files for
  the few people who want to explore the world offline, etc. Let me
  know if you want more.
(Replies frozen) (Thread)
[User Picture]From: kiodane
2011-06-17 04:04 pm (UTC)
Ugh, some of those lines got wrapped... I'm not on my computer and it looks like the formatting got a tad messed up.

https://files.me.com/kioarthurdane/sdekbg
(Replies frozen) (Parent) (Thread)
[User Picture]From: xv
2011-06-17 04:07 pm (UTC)
Hey dude. :)
(Replies frozen) (Parent) (Thread)
[User Picture]From: kiodane
2011-06-17 05:41 pm (UTC)
Long time no see, my brotha'

I posted my response on applescript for exactly the reason they said on their cross-post that there was virtually no activity over here.
(Replies frozen) (Parent) (Thread)
[User Picture]From: xv
2011-06-17 04:10 pm (UTC)
Also note that if OP wants to incorporate this into an AppleScript for whatever reason, there is the wonderiferous 'do shell script' command. http://developer.apple.com/library/mac/#technotes/tn2065/_index.html
(Replies frozen) (Parent) (Thread)
[User Picture]From: kiodane
2011-06-17 06:17 pm (UTC)
You know, there are many occasions where I have used AppleScript for the "logic" part of an idea, and used the "do shell script" command as a means of getting around AS's ridiculous file/alias/POSIX minutia. Writing data to file is also done much faster and with less headache using something like

do shell script "echo \"" & stuffToWriteVariable & "\" >> ~/Desktop/output.txt"

Be aware of the escape character requirements of nesting one language inside another, however.
(Replies frozen) (Parent) (Thread)
[User Picture]From: johncoxon
2011-06-17 05:56 pm (UTC)
I'm really sorry, but I don't understand how that would help create an executable which I could run in order to switch between vanilla and modded Minecraft. Maybe I'm just being stupid, but I can't see anything in your script that checks to see whether a specific folder exists in order to get different things to happen if I'm already running vanilla to if I'm running the modded version, for instance.

Thanks very much though! :)
(Replies frozen) (Parent) (Thread)
[User Picture]From: kiodane
2011-06-17 06:10 pm (UTC)
You're right, there is a certain amount of "dumbness" that my script exhibits because to change your mind between mod or vanilla, you actually have to edit the script file and comment/uncomment the lines yourself.

But I think that a Minecraft server is more enjoyed and better managed if you're familiar with Terminal and bash. Applescript is something I love too, but for this it really seems bloated for what you're trying to accomplish.

I also did not mean to say that my above script was meant as a solution to your problem. In fact, it's the solution to MY problem, and I was making it available for you to consider in your journey.

To be honest, even though you explained your end goal, I didn't understand the point of detecting if a folder exists or not and to launch a different version based on that. Again, I think bash is better suited to your goal since I suspect the majority of Minecraft servers are running on Linux, and bash is a common element between Mac and Linux (and therefore more support from the MC server forums).

But XV provided support on your compilation error, something I couldn't do away from my Mac at the time I wanted to contribute my solution to my problem.

If you want to explore bash scripting, I recommend this guide: http://tldp.org/LDP/abs/html/
(Replies frozen) (Parent) (Thread)
[User Picture]From: johncoxon
2011-06-17 06:14 pm (UTC)
I'm not trying to run a server, I'm just trying to run a client. I just want something that, if I want to easily switch between the vanilla client and the modded version, I can use spotlight and hit enter, which seems like a fairly simple implementation of the thing I'm trying to do. It's easier than going to /minecraft/bin and renaming the files every time I want to switch over. So far I haven't had to use any Terminal at all in order to play Minecraft, so integrating this into a Terminal productivity flow would add steps that are unnecessary, as far as I can tell...

I am probably just being dense but I genuinely don't understand how your code would help me achieve the goal I have in mind!
(Replies frozen) (Parent) (Thread)
[User Picture]From: kiodane
2011-06-17 06:19 pm (UTC)
Ah, yes. I did misunderstand you.

I have no personal interest in running a modded client, and therefore cannot help you.

Sorry to waste your time.

Perhaps you could just keep two separate folders, each with their own version of the client. Then you as a human being can decide on which you want.
(Replies frozen) (Parent) (Thread)
[User Picture]From: johncoxon
2011-06-18 12:01 am (UTC)
Thanks for your help, nonetheless :)
(Replies frozen) (Parent) (Thread)
[User Picture]From: kiodane
2011-06-17 06:24 pm (UTC)
but I genuinely don't understand how your code would help me achieve the goal I have in mind!


In Terminal, you can generally get help on any command by typing "man ". Regular Expressions are an annoying thing to learn, however, but they are useful in other locations like Perl and BBEdit/TextWrangler searches.

A bash script is little more than a series of commands that you would otherwise execute in Terminal by hand.
(Replies frozen) (Parent) (Thread)
[User Picture]From: xv
2011-06-17 04:05 pm (UTC)
I have to agree bash is the way to go here, Applescript file handling is ... odd. That said, though, sometimes you want to handle files in an odd, esoteric, way. So, to get your script to compile (maybe function, I dunno) you would first want to translate from posix notation by changing "set this folder to ..." to "set this folder to posix file ... " You can run that as a script by itself to see what the result is. Assuming that translates well, finder might accept it, might not, you just have to fiddle with it. It might need to also have an "... as alias" tacked on the end.

As for the actual thing your compiler is hiccuping on there, that line:

set file "minecraft.jar" of thisFolder as "minecraft-original.jar"

...doesn't make sense. The word "as" is only for changing types, like "as text" or "as alias" not changing names. To change a name you would say something like:

set name of file "minecraft.jar" of thisFolder to "minecraft-original.jar"

...using the "set ... to" model, not "set ... as." Changing the grammar will make it compile. Again, I don't know if the Finder will behave as expected, you might check its dictionary, sometimes they prefer different commands.

Also, I believe Automator has Finder actions that can handle all this as well, if you'd rather not mess around with semantics. ;)
(Replies frozen) (Thread)
[User Picture]From: johncoxon
2011-06-17 05:54 pm (UTC)
I don't know if Automator can handle if statements, can it? That's why I was attempting to do it via AppleScript as opposed to Automator (I actually basically understand how to use Automator, as opposed to being a complete newbie with AppleScript).

Edited at 2011-06-17 06:01 pm (UTC)
(Replies frozen) (Parent) (Thread)
[User Picture]From: xv
2011-06-17 08:18 pm (UTC)
Ah, they told me they were going to add those. Looks like they just added variables and loops in my copy, no conditionals to be found, just filters. Well, you can still put them in by using the AppleScript action, but at that point it's more trouble than it's worth.

So, have we sorted your script yet? (haven't read the comments above)
(Replies frozen) (Parent) (Thread)
[User Picture]From: xv
2011-06-17 08:59 pm (UTC)

try this

tell application "Finder"
get the home as alias as text
set thisFolder to the result & "Dropbox:Minecraft:minecraft:bin:" as alias
if the name of thisFolder's folders contains "minecraft-mods.jar" then
set the name of folder "minecraft.jar" of thisFolder to "minecraft-original.jar"
set the name of folder "minecraft-mods.jar" of thisFolder to "minecraft.jar"
else
set the name of folder "minecraft.jar" of thisFolder to "minecraft-mods.jar"
set the name of folder "minecraft-original.jar" of thisFolder to "minecraft.jar"
end if
end tell
(Replies frozen) (Parent) (Thread)
[User Picture]From: johncoxon
2011-06-17 10:33 pm (UTC)

Re: try this

Okay, so I officially love you, that works exactly how I want it to, thanks so much :)

I take it that in order to use thisFolder as a placeholder for the path you're handling, you need to put 'as alias' at the end? Also, I notice that you've used colons to denote filepath as opposed to slashes, I presume that's just a convention of which I was unaware?

You've written the if statement in order that AppleScript checks the names of the folders rather than the way I had done it in my original script – is there a reason that this works better than the way I was trying to do it? I can see why my renaming wouldn't have worked, that was clearly me being stupid! :)

Hope you don't mind me asking questions, but I like knowing where I was going wrong and it seems to me that asking is the best way to achieve that.
(Replies frozen) (Parent) (Thread)
[User Picture]From: xv
2011-06-17 11:50 pm (UTC)

Re: try this

Yeah, slashy paths are POSIX, which are relatively new to AppleScript. There are some very rudimentary commands for dealing with those, but AppleScript prefers the older HFS-style paths, which use colons. An alias is always an HFS path. The reason we have to specify that these things are aliases, though, is that Finder prefers to use an even more arcane form of references, which are long cumbersome sentences like "the file of the folder of the disk of whatever."

As to your other question, I tried using the "exists" property, but the Finder kept reporting "false" no matter what. I guess you could chalk that up to one of AppleScript's many "unpainted corners," or places where magical things could happen, but for whatever don't. In my experience, I just work with the data that the program is willing to give up. In this case, I'm asking for a list of names, and then looking through the list to find a match.
(Replies frozen) (Parent) (Thread)
[User Picture]From: johncoxon
2011-06-18 12:00 am (UTC)

Re: try this

Thanks :)
(Replies frozen) (Parent) (Thread)