How to set an alias to open a file in Bash - windows

I am trying to set an alias to open an Excel file from a directory on Windows 7. Each time I use the alias it just opens a Windows Explorer window in the root directory.
I am entering:
alias openExcel="explorer /z/Company/AP/PR/thisExcelFile.xlsx"
I have also tried:
alias pr="cd /z/Company/AP/PR/"
Then:
alias openExcel="explorer thisExcelFile.xlsx"
Running these commands manually opens the Excel file. But using the commands through an alias just opens Windows Explorer in the root directory.
I have also tried combining the bottom two commands in a function but that didn't work either.
Can alias' not be used to open files like this? Is there some exception to how an alias works when you use the 'explorer' command on Windows?

Your commands are fine - you're just running the wrong application.
The first part is the command/executable, and the second is the argument. You just need to open the file with Excel.
// This runs "Windows Explorer", with the parameter "thisExcelFile.xlsx"
explorer thisExcelFile.xlsx
// This runs "Microsoft Excel", with the parameter "thisExcelFile.xlsx"
excel thisExcelFile.xlsx
You can do the same with winword myWordDoc.docx, or firefox www.google.com.
It may also be a good idea to quote the argument, just in case:
excel "thisExcelFile.xlsx"

Related

How to run Script per shortcut

I have a Terminal cmd running that executes a function from the .bash_profile file (as described here).
From the description, I added the function to the ~/.bash_profile (can be found here /home/user/.bash_profile)
From my Terminal everything works !
But how do I create a shortcut in Mac OS that executes this Terminal cmd ??
I tried Apple Automator (--> but error: Cmd not found)
I tried Alfred (--> but same error: Cmd not found)
How can I make this Terminal cmd execute from a keyboard-shortcut ??
Do I need to place the script-functions in another file (other than bash_profile ?) - or what is here to do ?
Here is an image of the Automator trial:
Place the functions in separate files, such as suspend-script.sh, resume-script.sh, and kill-script.sh; put them in a common directory like /home/user/scripts. To keep using them in your .bash_history, do:
# in .bash_history
source "suspend-script.sh"
source "resume-script.sh"
source "kill-script.sh"
Make sure you set the executable bit on each script (chmod +x /home/user/scripts/*.sh). Then, use the following for shortcut keybindings:
/home/user/scripts/suspend-script.sh thing_to_suspend
/home/user/scripts/resume-script.sh thing_to_resume
/home/user/scripts/kill-script.sh thing_to_kill
Since it's a shortcut to a function, you have to pick one script to suspend/resume/kill (the thing* above).
For example, if you wanted to suspend/resume/kill the top process, you would use the following:
/home/user/scripts/suspend-script.sh top
/home/user/scripts/resume-script.sh top
/home/user/scripts/kill-script.sh top
Open Automator, choose Application, add a Run Shell Script action and put in your Shell command between quotes (if you have a file, you can just drag and drop it).
Other than playing it, now you can save it (as an app anywhere) and even set the icon. Then you got an app and then you can define a keyboard shortcut for it in system-preferences.app
This should work, at least it does for me.
make sure your script is executable: sudo chmod u+x <your-scriptname>
and also add the filename extension .command to your filename. So the name of the script should be filename.command.
When you then click the script in Finder, it gets executed.
I hope that works!

How to create AutoHotKey which starts script (i.e .bat) using Cmder command line arguments?

I am trying to create an autohotkey which starts a script (i.e .bat):
#!g::
Run "Path\To\script\script.bat" debug
Return
The AutoHotKey works properly. However, I would like the .bat to be opened using Cmder instead of the default windows cmd.
I tried to set the Cmder as the default cmd by going to Settings-> Integration -> Default term and by checking the "Force ConEmu as default terminal for console applications", but this did not change anything in my particular case, and the script is still being ran in windows cmd.
Assuming Cmder can take a command line parameter, make it explicit:
Run "Path\To\exe\Cmder.exe Path\To\script\script.bat" debug
Else, run Cmder and then use AHK to file open the script.
EDIT: Based on OP's solution, could try:
Run "Path\To\exe\Cmder.exe /Task ""Path\To\script\script.bat debug"""
or
Run "Path\To\exe\Cmder.exe /Start ""Path\To\script"" /Task ""script.bat debug"""
The exact syntax and whether and where to surround things with quotes is tbd, but try and see.
In the end I used below script to make it work:
; Win+Alt+y - Start script
#!y::
Run "d:\cmder\Cmder.exe" "d:\somePath\FolderContainingTheBat\"
sleep 1000
send script.bat debug{enter}
Return
EDIT
Another solution for this does not involve AutoHotKey and works only with default windows cmd. I did the following:
Create a .bat file which contains the following
script.bat debug
Create shortcut for the newly created bat file and assign to it a "keyboard shortcut": Right click on the created shortcut -> Properties -> "Shortcut" tab -> Shortcut key
I made it work with this
Run, C:\path\Cmder\vendor\conemu-maximus5\ConEmu64.exe C:\path\to\script.exe

Executing a command-line .exe file

I have a .exe file converted from a .jar.
It is a command based application, so I have to start it with a batch script. Here is the batch script:
#echo off
cd C:\desktop\plant-text-adventure-win
start planttextadventure
pause
When I double click on the batch script, this happens: Windows could not find 'planttextadventure'. Please confirm if you have input the correct name and retry.
I don't know what is happening, I have no idea about cmd as I use Mac, but I can confirm I have an executable called planttextadventure.exe in a folder called plant-text-adventure-win.
You should test your batch file by executing it within a shell.
Simply enter within the start menu the command cmd to open up a shell. Within this black box you could now simply enter the commands from your batch script and lookout for some error message.
If you look at your script I would guess that the cd command (to change the current directory) is not correct. Maybe you should replace it with
cd %USERPROFILE%\Desktop\plant-text-adventure-win
because the desktop folder is on a default installation not directly under the root drive but within the user profile available.
Another solution to get this thing to work, is by opening the windows explorer, going to the .exe file you wish to execute and drag & drop the .exe file with a right mouse click onto the desktop.
Then a context menu appears and you select the option Create shortcut here.

Open an ipython notebook via double-click on osx

I've downloaded a couple of ipython notebooks, and I'd like to open them in browser tabs without navigating to the directory I've downloaded them to and running ipython notebook notebook_name.ipynb.
I realize this probably means I'm lazy, but it seems like a common use case to me. Am I missing something obvious?
Use Pineapple application for opening and working on your IPython/Jupyter notebooks. It is pretty cool.
Update:
Now there is nteract, which is a new jupyter-like Desktop app. After installing it, make it the default app for opening .ipynb files. Then just double-click any notebook to start it right away.
I have found a nice way using Automator (Oct. 2017; with information from here):
open Automator and create a new Application
to add Run Shell Script drag and drop it from the list; might need these settings Shell: /bin/bash and Pass input: as arguments
insert the code below; if necessary adjust the path to jupyter
Code
#!/bin/sh
variable="'$1'"
the_script='tell application "terminal" to do script "/usr/local/bin/jupyter notebook '
osascript -e "${the_script}${variable}\""
save the script as an application (!)
try to open a .ipynb file and change the default app to this newly created one.
notes
This will open a terminal and run the jupyter notebook command, such that you can interrupt and stop the notebook-server from there.
Also note that you cannot test the app like that in Automator, but need to add the Get Specified Finder Items and insert some test notebook there (just for testing purposes).
As pointed out in the comments, there are two more notes:
To avoid spamming your browser history with notebooks, you can start your notebooks in incognito/private mode: Run jupyter notebook in incognito window
If you want to run notebooks in one server and don't mind an extra tool, Sachit Nagpal has pointed out (thank you), that one could also use nbopen. To use this workflow just replace "/usr/local/bin/jupyter notebook ' with "nbopen '. Any other tool should work alike.
pip install nbopen.
open Automator, create new Application
Drag'n drop Run Shell Script
Change Pass input to as arguments
Copy/paste this script:
variable="'$1'"
the_script='tell application "terminal" to do script "nbopen '
osascript -e "${the_script}${variable}\""
Save the new application to Applications directory as nb_open
Right click any ipynb file and select "Open with > Other" and select the nb_open in the Applications folder. Don't forget to check "Always Open With".
Select an ipynb file, get info (command + i) > Open With (select nb_open if not selected already) > Click Change All.... Done.
The application posted here worked pretty well for me: http://bioequity.org/2013/11/16/ipynbviewer/
You also need to download iTerm2, as described on that page.
Note that this doesn't work if there are spaces in the filename, but you can modify it so that it works with spaces in the filename. Control-click on the iPyNbViewer.app and select "Show package contents". Edit the file Contents/Resources/Scripts/main.scpt. Change three instances of "POSIX path" to "quoted form of POSIX path". Now it will work with spaces in the filename.
To set all of your .ipynb files to open with the app, you'll need to Get Info (command-I) on one of the files and select the iPyNbViewer app to open all .ipynb files.
It would be great if this was the default behavior of double-clicking on an iPython notebook file...
I came up with a way of doing it on Ubuntu. (It works for me but I can take no responsibility). It's explained here. In a nutshell, you need to create a new MIME type, then write a script that works as the app that launches them:
#!/bin/bash
netstat -tln |grep "8902"
# if not found - equals to 1, start it
if [ $? -eq 1 ]
then
ipython notebook / --no-browser --port=8902 &
sleep .5
fi
xdg-open http://localhost:8902/notebooks$1
This always opens the notebook server on port 8902, but first checks whether there is already a server running, and, if so, uses it.
Then you can use ubuntu tweak to select your script as a default application for the MIME type "IPython Notebook" you just created.
Not very elegant, but worth it, in my opinion.
PyCharm now supports Jupyter ipynb files:
which is from the documentation https://www.jetbrains.com/help/pycharm/editing-jupyter-notebook-files.html.
But I think this feature is only available in the Professional version now; hopefully it will be added to the Community version in the future.
Look at this link.
Put a bash script in the folder where you keep your ipython notebooks and simply double click it to open up a notebook instance. From the link above, the bash script has just:
path=$0 # path is path to this file
cd ${path%/*.*} # clip off the file name to get directory path and cd
ipython notebook --pylab inline
Finally, you need to chmod u+x the script to make it executable and you're done.
I have used the command line application 'nbopen' and put it in a Platypus wrapper to get drag'n drop and double click opening on Macos. 'nbopen' is downloadable using 'pip'
It works well when used as described above by DanHickstein.
Only problem with my code is that it requires the full path to the nbopen command. I know I should be able to use 'which nbopen' somehow but can't get it to work.
Heres my Platypus code:
#!/bin/bash
# Opens ipynb files in a Jupyter Notebook
# echo $1
# $1 is the path of the dropped file
/Users/robw/anaconda/bin/nbopen $1
# Based on an idea from
# https://www.quora.com/Is-there-a-straightforward-way-to-open-an-IPython-Notebook-in-Windows-by-double-clicking

How do I make my Perl scripts act like normal programs on Windows?

I want my Perl scripts to behave just like any other executable (*.exe file).
When I double-click on myscript.pl I want it to execute instead of opening in a text editor.
I want to run myscript.pl instead of perl myscript.pl.
I really want to run myscript instead of myscript.pl.
I want to run program | myscript instead of program | perl myscript.pl.
I want to be able to run my script via drag & drop.
There are a number of changes you have to make on Windows to make all of
these things work. Users typically stumble upon things that don't work one at
a time; leaving them confused whether they've made an error, there's a bug in
Perl, there's a bug in Windows, or the behavior they want just isn't possible.
This question is intended to provide a single point of reference for making
everything work up front; ideally before these problems even occur.
Related questions:
How do I make Perl scripts recognize parameters in the Win32 cmd console?
Running a perl script on windows without extension
Perl execution from command line question
How can I read piped input in Perl on Windows?
Perl on Windows, file associations and I/O redirection
How do I create drag-and-drop Strawberry Perl programs?
Note: The actions below require administrative privileges. For
steps utilizing the command prompt it must be launched via "Run as
administrator" on Windows Vista / Windows 7.
Associate *.pl files with perl
Run the following commands at a shell prompt:
assoc .pl=PerlScript
ftype PerlScript=C:\bin\perl.exe "%1" %*
Replace C:\Perl\bin\perl.exe with the path to your Perl installation. This
enables you to run myscript.pl instead of perl myscript.pl.
Default install locations are:
ActivePerl: C:\Perl
Strawberry Perl: C:\Strawberry
Add .PL to your PATHEXT environment variable.
This makes Windows consider *.pl files to be executable when searching your
PATH. It enables you to run myscript instead of myscript.pl.
You can set it for the current cmd session
set PATHEXT=%PATHEXT%;.PL
To set it permanently (under Windows Vista or Windows 7)
setx PATHEXT %PATHEXT%;.PL
Under Windows XP you have to use the GUI:
Right-click My Computer, and then click Properties.
Click the Advanced tab.
Click Environment variables.
Select PATHEXT, then click Edit.
Append ;.PL to the current value.
Make I/O redirection work
I/O redirection (e.g. program | myscript) doesn't work for programs started
via a file association. There is a registry patch to correct the problem.
Start Registry Editor.
Locate and then click the following key in the registry:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
On the Edit menu, click Add Value, and then add the following registry value:
Value name: InheritConsoleHandles
Data type: REG_DWORD
Radix: Decimal
Value data: 1
Quit Registry Editor.
Warning: In principle, this should only be necessary on Windows XP. In my experience it's also necessary in Windows 7. In Windows 10 this is actively harmful—programs execute but produce nothing on stdout/stderr. The registry key needs to be set to 0 instead of 1.
See also:
STDIN/STDOUT Redirection May Not Work If Started from a File Association
Perl Scripts on Windows 10 run from Explorer but not Command Prompt
If patching the registry isn't an option running program | perl -S myscript.pl
is a less annoying work-around for scripts in your PATH.
Add a drop handler
Adding a drop handler for Perl allows you to run a Perl script via drag & drop;
e.g. dragging a file over the file icon in Windows Explorer and dropping it
there. Run the following script to add the necessary entries to the registry:
use Win32::TieRegistry;
$Registry->Delimiter("/");
$perlKey = $Registry-> {"HKEY_CLASSES_ROOT/Perl/"};
$perlKey-> {"shellex/"} = {
"DropHandler/" => {
"/" => "{86C86720-42A0-1069-A2E8-08002B30309D}"
}};
Convert your perl scripts into batch files using pl2bat once they are ready to be run by users.
The trick works through the perl -x switch which, according to perldoc perlrun, makes Perl search for the first line looking like #!.*perl.
After following the instructions in the accepted answer, a double click still led to .pl files opening with Notepad in Windows 10 — even when perl.exe was set as the default file handler.
After finding Jack Wu's comment at ActivePerl. .pl files no longer execute but open in Notepad instead I was able to run perl scripts on double-click as such:
Select and right-click a .pl file
Use the "Open With" submenu to "Choose another app"
Select "Always use this app to open .pl files" (do this now – you won't get the chance after you have selected a program)
Scroll to the bottom of the "Other options" to find "More apps", and select "Look for another app on this PC"
Navigate to C:/path/to/perl/bin/ and select Perl5.16.3.exe (or the equivalent, depending on which version of Perl you have installed: but not Perl.exe)
Then the Perl icon appears next to .pl files and a double-click leads to them opening in Perl every time, as desired.
I tried the assoc and ftype methods and they didn't work for me.
What worked was editing this registry key:
Computer\HKEY_CURRENT_USER\Software\Classes\Applications\perl.exe\shell\open\command
It was set to:
"C:\Perl64\bin\perl.exe" "%1"
When it should be:
"C:\Perl64\bin\perl.exe" "%1" %*
It is the same content as the ftype, but for arcane windows reasons, I had to set it there too.
Like some others, I had set 'assoc' and 'ftype', but also had set Notepad text editor via the GUI, and when I tried to execute a script via the command line, Windows invoked Notepad to edit the script instead of running my script.
Using the GUI to instead point the .pl file association to the script-running executable was not much of an improvement, since it would invoke the executable on my script, but would pass no command-line arguments (even when I invoked my script from the command line).
I finally found salvation here which advised me to delete some registry keys.
Key quote:
"The problem is that if you have already associated the program with the extension via the Open With dialog then you will have created an application association, instead of a file extension association, between the two. And application associations take precedence."
In my case, following the instructions to use RegEdit to delete
HKEY_CLASSES_ROOT \ Applications \ perl.exe
where perl.exe is the name of my Perl executable, and then also deleting:
HKEY_CLASSES_ROOT \ .pl
seemed to solve my problem, and then (after re-executing 'assoc' and 'ftype' commands as shown in other answers) I could then execute scripts from cmd.exe and have them run with access to their command-line parameters.
Some other related information here.

Resources