How to download a file on apple script - applescript

I want to make an applescript program to make a minecraft server for anyone.
I need to download a file from http://dl.bukkit.org/latest-beta/craftbukkit-beta.jar.
Here is my current code:
if the button is "Beta" then
do shell script "curl -o ~/Desktop/BukkitServer http://dl.bukkit.org/latest-beta/craftbukkit-beta.jar"
end if

You curl was a bit off + you need to make sure that folder "BukkitServer" exists too.
tell application "Finder"
if not (exists folder "BukkitServer" of desktop) then
make new folder at desktop with properties {name:"BukkitServer"}
end if
end tell
do shell script "curl -L http://dl.bukkit.org/latest-beta/craftbukkit-beta.jar -o ~/Desktop/BukkitServer/craftbukkit-beta.jar"

Related

How to open an EMACS file in OS X by double-clicking on it, using the `emacsclient` command?

I want to open an org-mode file selected in the Finder, by double clicking on it. But since I use Emacs in daemon-mode, I want to use the emacsclient command for that.
Thus the primary idea was to wrap the command emacsclient -c posixPathToFile in an AppleScript App to open it.
tell application "Finder"
set fileAlias to the selection as alias
set fileName to name of fileAlias
set posixPath to POSIX path of fileAlias
end tell
-- tell application "Emacs" to activate
try
do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
end try
I know some set commands are not needed. Let's assume this script is saved as Xemacs.app and that I associate this app to always open .org file.
Using this App does not work by double-clicking on the file, but rather if I select the file in the Finder and then call the Xemacs.app independently. Why ? I'm not confident enough with AppleScript to figure out what happens.
So the workaround was to use the Automator service
on run {input, parameters}
set posixPath to POSIX path of input
tell application "iTerm" to do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
return input
end run
The service is saved as 'Open in Emacs'
Now selecting a file and right-clicking and callig Service > "Open in Emacs" works and opens the file.
What is wrong with the first approach ?
ok, I solved my issue. The problem comes from my misunderstanding of the difference between ScriptEditor and the Automator. If I use the Automator to create an App and use the former script instead of creating an App using the ScriptEditor, then it works as expected.
One can simplify the process by creating an App in Automator and running a shell script instead of wrapping the command in Ascript.

Applescript run bash script from same directory

I'm trying to build an AppleScript to launch my shell script.
Path structure is as follows
/Users/ryan/myscript/
applescript.scpt
bash.sh
My AppleScript is as follows:
tell application "Terminal"
set folder_path to path to me
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
activate
end tell
Problem is the 'path to me' is not always returning the correct path. When executed using the Mac cd/dvd autoplay behavior folder_path is equal to:
disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:
Is there is a better way of getting the folder path?
If this Script is in a static location, you can do this:
do shell script "/bin/bash" & POSIX path of (path to current user folder) & "myscript/bash.sh"
Path to me refers to the location of the applescript that is running. So if your script is on a disk then it will reference the location on the disk where the script is saved
if it is expected that the shell script will always exist in a folder called "myscripts" that exists in the current user folder then you could use path to current user folder and build out from there
set user_folder to path to current user folder
set folder_path to quoted form of POSIX path of (("" & user_folder & "myscript"))
tell application "Terminal"
activate
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
end tell
Is there a reason why you have to store the shell script in a separate file? Typically, you would put it inline, within the AppleScript code. As far as I know, the “do shell script” command only operates on text, not on a script at a file path. If you give it a variable that contains a path, it will try to run that path as a command. It won’t run the contents of the file as a command.
Here is an example of an AppleScript that runs an inline shell script and puts the results in TextEdit:
property theShellScript : "#!/bin/bash
echo Hello World"
tell application "TextEdit"
activate
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
… you can of course replace the above shell script with the contents of your own shell script.
If you do need to keep the script in a separate file, the best way to do that is probably to save your AppleScript as an Application, and put the shell script within the Application bundle. “Path to me” is the path of the application that is running the script — not to the script itself — but if you save your AppleScript as an Application, then it runs its own script, and “path to me” works as you originally expected.
Here is an example of an AppleScript that runs a shell script contained within a file that is stored within its own application bundle:
property theApplicationPath : the path to me as text
property theShellScriptPath : theApplicationPath & "Contents:Resources:Scripts:bash.sh"
tell application "TextEdit"
open alias theShellScriptPath
set theShellScript to the text of document 1
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
With the above script Copy/Pasted into a new document in AppleScript Editor, hold down the Option key and choose File ▶ Save As, and in the Save dialog box, on the File Format pop up menu, choose “Application” and of course give your application a name and click Save. Then in Finder, navigate to where you Saved your application, and 2-finger tap (or right-click) on your application and choose “Show Package Contents.” That opens your application up as a folder, exposing the file system within. Put your shell script file named “bash.sh” inside the folder “Contents/Resources/Scripts” within your application and then close the window that represents your application.
Now when you run your application from anywhere in the file system, it will still be able to find and run its incorporated shell script.

Apple Script - Selecting files, exclude type, zip it

I need to zip the content of a folder (and all the subfolders) for hundreds of folders.
Is it possible to run a command that takes all the files of a specific folder (prompt), except all the files that have a .fla extension and zip this content into one zipfile?
Right now I am copying the folder, search for all the .fla-files, then select all the files inside the folder (I have the to zip the content, not the folder) and create a zip of it (takes way too long.
I know that it is possible to use Apple Script to delete and copy files. But does this also work in the above mentioned order + zipping?
Alright, so I was still kind of stuck with this issue.
I created a Bash Script, that is executed via an Applescript executable File that has only one line of code:
do shell script "/Volumes/Work/createZipFile.sh"
The Bash Script opens Applescript which lets me prompt a folder (I know, kind of silly to open AS to run a Bash Script that runs AS). The variable is then used to zip this folders content without the .fla files.
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to zip!"
end tell
return (posix path of myfolder)
EOT`
cd $myFolder
zip -r ZipMe.zip . -x ".fla"
echo "A zip File has been created"
So this script does actually work for some folder I try to zip.
But unfortunately not for every folder I chose. Sometimes (no idea why) it seems like it can not find the folder I chose with the prompt, so I starts (at least the zip-process starts running like crazy and doesn't stop) zipping my whole drive.
Any ideas what could be wrong?
In case anybody wants to use this script (which I highly doubt ;)), here is my final version of it.
#!/bin/bash
#Opens an applescript prompt window to select a folder
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to Zip!"
end tell
return (posix path of myfolder)
EOT`
# Terminate if the path is empty (canceled)
if [ -z "$myFolder" ];
then
#echo "Chose a folder!"
exit 0
else
#Change the directory to the above selected folder
cd "$myFolder"
# Creates a ZipFile with todays date of the selected folder, neglecting the after -x listed filetypes
zip -r ZipFile_`eval date +%Y_%m_%d`.zip . -x "*.fla*" "*.AppleDouble*" "*.DS_Store*"
#echo "A zip File has been created"
fi
Your first step should be to figure out what "Kind" of file the .fla is. To do this, run this script and select one of your .fla files:
tell application "Finder"
set theFile to choose file
display dialog (kind of theFile) as string
end tell
And then to get all of the files BUT that type in any folder, you can run this script (Replacing "Plain Text" with whatever type your .fla's turn out to be):
tell application "Finder"
set thePath to choose folder
set theFiles to get every file of folder thePath whose kind is not equal to "Plain Text"
end tell
from there it's just a matter of zipping. After doing some quick googling it looks like the easiest way to zip from applescript is by using do shell script, which shouldn't be that bad now that you have all the files you need in a nifty little array. If you're going for speed though, I might suggest moving this whole project over to bash. That should also simplify things quite a bit. Best of luck!

Applescript to open application from CD Drive

I have a flash program on CD which i need to auto run when Cd insert in drive.
tell application "Finder"
activate
if exists disk "MYRIAD" then
tell application "Mac"
activate
end tell
end if
end tell
Meriad is my CD name and Mac is my flash application. it fire up the Mac application when i test it from desktop but when i burn it on CD it just open FINDER but nothing else.
Now i have also written code in my flash fla in second line to execute Mac application.
stop();
fscommand ("exec", "Mac");
fscommand("fullscreen", true);
fscommand("allowscale", true);
fscommand("showmenu", false);
i dont understand where i am going wrong.
please somebody help.... Thanks in advance.
There is a couple of ways you can do this.
But try this.
tell application "Finder"
activate
if exists disk "MYRIAD" then
tell application "MYRIAD:Mac.app" to activate
end if
end tell
Make sure you set the correct path in the "tell application "MYRIAD:Mac.app" to activate"
line.
separate each directory with a colon ":" but do not put one at beginning or end of the path to the app.
In my example the fist directory is the CD itself. The no other directories leading to the app.
Also make sure you name the app as it appears in the finder.

How can I convert bash's command substitution and pipe to an Applescript?

I need help converting this simple shell script to an apple script.
The point being because it is to be used in an Automator workflow, and so I need the Terminal window to be open, which cannot be done using a shell script.
The shell script is as follows:
java -classpath `dirname "$1"` `basename "$1" | sed "s/.class//g"`
This gets the location of the file, and then the name of the file, and then strips away the file extension of ".class", and then runs it using the Java command. So for example it would generate the following command:
java -classpath /users/desktop/ filename
I need to convert this command so that it works with Applescript so that I can then see the application run in the Terminal window. It would start like the following:
on run {input, parameters}
tell application "Terminal"
activate
do shell script "java -classpath path/to/ file"
end tell
end run
How can I port the text transformation to Applescript?
The only issue I'm seeing (right now) is to change do shell script to do script. Other than that, you've started it correctly. I'm assuming you want to pass (a) file reference(s) to the shell script. It's fairly simple...
set these_files to (choose file with multiple selections allowed)
repeat with this_file in these_files
tell application "Finder" to if the name extension of this_file is "class" then my do_shell_script(this_file)
end repeat
on do_shell_script(this_file)
tell application "Terminal" to activate --makes 'Terminal' the frontmost application
--'do shell script ...' goes here
--To refer to a file/folder for a 'do shell script', do something like the command below...
--do shell script "mdls -name kMDItemLastUsedDate " & quoted form of the POSIX path of this_file
end do_shell_script
I don't know AppleScript at all, but I suppose you could simply call your existing shell script in the do shell script line, instead of trying to redo the string manipulation in AppleScript.
Note:
It looks like you want to be able to invoke Java classes by click (or drag-n-drop or such). Your method will only work for classes in the anonymous package, i.e. without any package ...; declaration in the beginning of the source code. For others, you might try to find out in which package they are. I have no idea how to do this. Distributable programs should be in jar archives, anyway, where you should be able to start it with java -jar ....)

Resources