How do I test for a folder existing in Apple Script? - directory

I am trying to run "cd Desktop" in the terminal if the Desktop folder exists. Here is my current code:
tell application "Terminal"
activate
try
if exists folder "Desktop" then
do script "cd Desktop" in tab 1 of window 1
end if
do script "java -jar /Users/Harry/Desktop/Candle.app/Contents/candle.jar" in tab 1 of window 1
on error
if exists folder "Desktop" then
do script "cd Desktop"
end if
do script "java -jar /Users/Harry/Desktop/Candle.app/Contents/candle.jar"
end try
end tell
I am very new to apple script, so I don't know why this calls a syntax error when saved/compiled. Can anyone help??? Thanks.

AppleScript is a fairly minimal language, and relies on other applications to perform stuff that it doesn't know about.
You are getting the error because Terminal doesn't know what the term folder is. Something like Finder or System Events can be used to handle file objects, and you can also coerce the path to an alias, which will error if it does not exist - note that your usage of the term exists in this case would be from the standard suite, which is for general objects, so it will return true since the string does exist.
Also note that each do script statement will be in its own window unless otherwise specified. I think you are looking for something more like:
set theFolder to "/Users/you/Desktop"
tell application "Finder" to set folderExists to exists (folder theFolder as POSIX file)
if folderExists then
tell application "Terminal"
activate
try
do script "cd Desktop" in tab 1 of window 1
on error errmess
log errmess
do script "cd Desktop"
end try
delay 0.05
do script "echo testing" in tab 1 of window 1
end tell
else
beep
end if

Related

Short cut for invoking shell script in unix

I have below two file to start and stop my spring-boot application. Is it possible to have this installed as server etc in unix? so that I can just type start app or stop app to start or stop my application from any location?
startApplication.sh
stopApplication.sh
You can always define alias in your bash, do as below:
sudo vim ~/.bashrc
go at the end of file and add this line
alias start-app='bash /<path-to-script>/startApplications.sh'
save and exit and resource it with source command
source ~/.bashrc
now if you type in your terminal start-app it will execute your script. create one for stop-app too.

Get system directory path

I want to upload an image file using an AutoIt script:
WinWaitActive("Open")
Send("D:\sprint8execution\gGastro-mvn\tmp.png")
Send("{ENTER}")
How to give the system-defined path in the script so that if the script runs on any other machine it goes to applicable directory and fetches the image from there?
Have a look to the AutoIt macros. #ScriptDir is the directory that includes the current running script.

Schedule to run a executable jar file on windows 7

I created a task in task scheduler on Windows 7 system and made it repeatable every 10 minutes.
In program, i selected the executable java jar file. But it does not run the jar file at the scheduled time.
When i double click and run the jar file, it runs as desired.
The Jar just pops up a dialog box.
Any inputs as to where i am going wrong is appreciated.
Firstly make sure Java is set in the enviroment PATH by opening cmd.exe and typing java -version. If you get back the java version, then you are fine. (If not see Update the PATH Environment Variable (Microsoft Windows))
Then create a text file, save it as run.bat and type inside:
java -jar <insertjavajarnamehere>.jar
Make sure the bat is in the same directory as your jar file.
Now go in Windows Task Scheduler > Create Basic Task > ... >Start a program > and browse for your .bat file.
Also, set Start in to the path where your .bat and .jar are located. Create your task and it should run fine afterwards.
Edit: To avoid the shell being visible a simple trick is to create a VBS file
Create a run-invisible.vbs, and type:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("C:\Users\pathtobat\run.bat"), 0, True
And schedule that instead of the bat (make sure you update the path to your bat in the vbs file)

Update current working directory after vim netrw exit

Here's the scenario.
I am on the unix command line (in home directory). I want to browse the directory through
$ vim .
thus opening the vim netrw.
Now I am browsing the directory using the netrw.
What I want here is that when I exit vim netwr, I want my previous current working directory (in this example the home directory) to now become the directory I was previously in vim netrw.
Example:
step 1. now in home directory
step 2. vim . (thus opening vim netrw)
step 3. go to any directory (~/my/other/folders)
step 4. :q (to exit vim)
step 5. (here, I want my previous directory to now become ~/my/other/folders
any ideas on how to do it? I was thinking of doing something in .vimrc but I dunno how. Been into google search, but found nothing valuable.
A possible solution would be to change the current work directory while in netrw by pressing c, and spawn a new shell from the folder you're in by issuing :shell
So it would look like:
vim .
Navigate to the desired folder...
c
:shell
And there you are in a shell in the current folder you were in netrw.
And when you exit that shell, you fall back to where you were in netrw and can continue using the explorer.
I don't think it's possible at all. Every command executed via system('command') or :!command is executed through a subshell, not through the shell that started Vim so I don't see how you could alter the host shell in any way.
But I smell an XY problem here. What is your goal?
Do you want to be able to execute some commands on the files you just edited and you want to be in their directory? If so, do you know about :sh? :!command?
Do you want a "graphical" file explorer for your shell? If so, do you know vifm? Ranger? Midnight Commander?
To add to Wadih's answer, you can put this in your .vimrc file:
let g:netrw_keepdir=0
This means the working directory will be automatically updated and you don't need to press c each time.
So after navigating to a folder in netrw, all you need to do is do this:
:sh
And this opens the terminal in the current folder.
From the netrw help file:
By default, g:netrw_keepdir is 1. This setting means that the current
directory will not track the browsing directory. (done for backwards
compatibility with v6's file explorer).
Setting g:netrw_keepdir to 0 tells netrw to make vim's current directory
track netrw's browsing directory.

How do I run a .bat file without a terminal (remaining open)?

I want to run a jar file with a .bat (the jar file doesn't seem to want to open on its own but thats a different issue for now) but as the java file runs for a long time, the command prompt remains open (while the .bat/.jar is still running)
I do not want this.
I read somewhere that you can use a .cmd file and the command(s):
cmd /c bat.bat
exit
To run a bat file without a command prompt. But that isn't working for me. When I click the .cmd program it just opens a command promopt and keeps printing "cmd /c bat.bat exit" over and over in a loop.
What am I doing wrong, was my .cmd command wrong? Is there another way to run a .bat without a command prompt remaining open?
Thanks alot.
From here:
Save the following as wscript, for instance, hidecmd.vbs after
replacing "testing.bat" with your batch file's name.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false
The reference is here
http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

Resources