Does drag and drop leave the filename in an accessible place (macos)? - bash

After I drag-and-drop a file, say from Finder, to say, Terminal running macos bash, is that value still accessible somewhere via a keyboard binding or an environment variable or some function or command? I know I could kill and yank the command line after the drag-and-drop lands on the command line, but I'd like something more direct if possible.

If you drag a file from Finder to the terminal the file name along with its path is displayed on the command line. It is not stored in any variable unless you write a code or script to read the file path and name. If you hit return after the name prints on the command line you'll probably get a "Permission denied" message (unless the name is an executable) but the path and file name will then appear in the history of commands. Typing history on the command line will show you the command you attempted to run. While not stored in a variable you should have temporary access to the info thru history.

Related

bash: character that fires the execution of an user defined program

Is it possible in bash while typing a shell command to have a characters that fires an user defined program?
For example, the user types at shell prompt
cp
and then types a special character that fires a program allowing to navigate the file system to find the copy command source file.
The programs return the selected file name that is appended to shell cp command.
Now the user types again the special character that fires the same program as above to navigate the file system for finding the destination file or folder.
At this point the command is ready to be sent to bash for execution

Vim stuck in "3 files to edit"

When opening a file with vim instead of putting the whole path I accidentally just wrote the path to the directory. So instead of:
vi myDirectory/myFile.txt
I put
vi myDirectory
Now vim has taken this to mean that I want to edit all 3 files in this directory and is stuck in that mode. I can quit vim and return to the console, but when I try to open the file with the full path vi myDirectory/myFile.txt I get a blank screen with "2 more files to edit" at the bottom. And then when I return to the console the line "3 files to edit" is printed.
How can I exit this situation so that I can continue to use vim normally and edit my file?
When you attempt to :edit a directory (or pass one as a command-line argument), Vim usually shows a directory listing and lets you browse files, courtesy of the :help netrw plugin. Vim does not automatically select all files from a passed directory.
Based on your report of having additional files also when the desired file's full path is passed, I rather suspect that you somehow have two additional "hidden" arguments to Vim, which Vim interprets as passed files.
This could be caused by a shell alias (in Bash, check type vi). Within Vim, you can list the additional files with :args; if they are indeed strange, non-filename strings, that might give you a clue (or at least some text to search for).

Permission Denied When Building in Sublime Text 3

I am attempting to utilize Lua on Windows 10 with Sublime Text 3. When I attempt to build and run a script I receive the following error:
lua: cannot open C:\Program Files (x86)\Lua\5.1: Permission denied
I have added full permissions to the folder.
I am only attempting to run a simple print statement:
print("hello")
The expected result is hello output in the results window.
The build system that ships with Sublime for executing Lua programs looks like this:
{
"cmd": ["lua", "$file"],
"file_regex": "^(?:lua:)?[\t ](...*?):([0-9]*):?([0-9]*)",
"selector": "source.lua"
}
The important aspect here is that the command to be executed is lua with a first argument that is the name of the current file. From the error message you're seeing, Lua looks like it's trying to execute a directory instead of the name of a program. Or if you will, if this was a directory permission problem I would expect it to tell you the name of the file that it can't access, not the folder than the file is stored in.
It's also suspicious that the name of the folder is the install location for lua itself, and that the error message seems to indicate that it's lua itself generating the error.
Based on all of this, my guess would be that you didn't save your Lua script before you executed the command. That would cause $file to expand out to an empty string, making the first argument empty. Since the build is using cmd, internally windows is being told specifically to run a program named lua with an empty string as it's first argument.
It looks like the interpreter first tries to put the current working directory onto the filename and then execute it, and since the file name is empty, it ends up trying to execute a directory, which is where the permission problem comes from.
Once you save the file the first time and it has a name, Sublime will automatically re-save it every time you run the build as long as Tools > Save all on build is checked; that option won't prompt you to save brand new files that don't have names yet, though.

navigate through folders using shell script

I am new to shell scripting. I have saved the script file as script_hdl in my home directory. From my home directory, I want to navigate using the script in the following order: cd ../../site/edu/ess/project/user/rark444
and then open a new tab from this new location in the terminal.
I used this as my script:
#!/bin/bash
alias script_hdl="cd ../../site/edu/ess/project/user/rark444"
I run the script like this
./script_hdl
But I don't see any response in the terminal. I feel I am missing something but I don't know what is it. Thanks in advance for your help.
You have two ways to change directory here.
Script
The first one is to write a script, in such a way that you can run other command after cd. It works without the alias command: let's say you remove it.
cd command is proper to the running process. When you execute your script, the following happen:
your shell spawns (forks as) a new shell process executing your code. The main process wait for its child to finish;
this new child process actually does change its own working directory with your cd command, then quits (it's over)
the original shell process stops waiting and prints the prompt again. But this process has not changed directory (only the child process did)
To perform what you want, (remove the alias command, then) call your script as follows:
source script_hdl
or with following shortcut:
. script_hdl
meaning that you want the instructions to run in the same shell process.
Alias
The second way to change directory is to use an alias. But you should not write your alias definition in a random script file, add it in your ~/.bashrc instead (this file is run each time you open a shell).
So:
alias script_hdl="cd ../../site/edu/ess/project/user/rark444"
to reload ~/.bashrc:
. ~/.bashrc
And then don't try to execute from the file, just launch your alias as if it was a normal command:
script_hdl
Looks like you are trying to set up an alias. You can do this by editing your .bash_profile file in your home directory (if it's not there you can create one and then run "source .bash_profile" after editing it) and make an entry like alias script_hdl='cd ../../site/edu/ess/project/user/rark444' and then run "script_hdl" from your terminal.
For more info on alias you can follow the link mentioned by Paul.
Make sure the spelling is correct as unix is case sensitive and that you have permissions. First try it on the command line to ensure that it works, if there is an error it will appear on the command line as sometimes scripts hide the errors and messages. If it works then copy the text to the script file and don't use alias.
Here is the correct usage of alias
https://en.wikipedia.org/wiki/Alias_(command)

Executing Shell Scripts from the OS X Dock?

How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?
You could create a Automator workflow with a single step - "Run Shell Script"
Then File > Save As, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.
The benefit to this is it's really simple to do, and you can very easily get user input (say, selecting a bunch of files), then pass it to the input of the shell script (either to stdin, or as arguments).
(Automator is in your /Applications folder!)
If you don't need a Terminal window, you can make any executable file an Application just by creating a shell script Example and moving it to the filename Example.app/Contents/MacOS/Example. You can place this new application in your dock like any other, and execute it with a click.
NOTE: the name of the app must exactly match the script name. So the top level directory has to be Example.app and the script in the Contents/MacOS subdirectory must be named Example, and the script must be executable.
If you do need to have the terminal window displayed, I don't have a simple solution. You could probably do something with Applescript, but that's not very clean.
On OSX Mavericks:
Create your shell script.
Make your shell script executable:
chmod +x your-shell-script.sh
Rename your script to have a .app suffix:
mv your-shell-script.sh your-shell-script.app
Drag the script to the OSX dock.
Rename your script back to a .sh suffix:
mv your-shell-script.app your-shell-script.sh
Right-click the file in Finder, and click the "Get Info" option.
At the bottom of the window, set the shell script to open with the terminal.
Now when you click on the script in the dock, A terminal window will pop up and execute your script.
Bonus: To get the terminal to close when your script has completed, add exit 0 to the end and change the terminal settings to "close the shell if exited cleanly" like it says to do in this SO answer.
I know this is old but in case it is helpful to others:
If you need to run a script and want the terminal to pop up so you can see the results you can do like Abyss Knight said and change the extension to .command. If you double click on it it will open a terminal window and run.
I however needed this to run from automator or appleScript. So to get this to open a new terminal the command I ran from "run shell script" was "open myShellScript.command" and it opened in a new terminal.
As long as your script is executable and doesn't have any extension you can drag it as-is to the right side (Document side) of the Dock and it will run in a terminal window when clicked instead of opening an editor.
If you want to have an extension (like foo.sh), you can go to the file info window in Finder and change the default application for that particular script from whatever it is (TextEdit, TextMate, whatever default is set on your computer for .sh files) to Terminal. It will then just execute instead of opening in a text editor. Again, you will have to drag it to the right side of the Dock.
In the Script Editor:
do shell script "/full/path/to/your/script -with 'all desired args'"
Save as an application bundle.
As long as all you want to do is get the effect of the script, this will work fine. You won't see STDOUT or STDERR.
I think this thread may be helpful: http://forums.macosxhints.com/archive/index.php/t-70973.html
To paraphrase, you can rename it with the .command extension or create an AppleScript to run the shell.
As joe mentioned, creating the shell script and then creating an applescript script to call the shell script, will accomplish this, and is quite handy.
Shell Script
Create your shell script in your favorite text editor, for example:
mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"
(this runs the w32 executable, using the mono framework)
Save shell script, for my example "StartKeepass.sh"
Apple Script
Open AppleScript Editor, and call the shell script
do shell script "sh /Volumes/Media/~Users/me/Software/StartKeepass.sh" user name "<enter username here>" password "<Enter password here>" with administrator privileges
do shell script - applescript command to call external shell commands
"sh ...." - this is your shell script (full path) created in step one (you can also run direct commands, I could omit the shell script and just run my mono command here)
user name - declares to applescript you want to run the command as a specific user
"<enter username here> - replace with your username (keeping quotes) ex "josh"
password - declares to applescript your password
"<enter password here>" - replace with your password (keeping quotes) ex "mypass"
with administrative privileges - declares you want to run as an admin
Create Your .APP
save your applescript as filename.scpt, in my case RunKeepass.scpt
save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case
Copy your app file to your apps folder
Exact steps to achieve that in macOS Monterey 12.3
Open Automator
File -> New
Choose Application
Go to Library -> Utilities
Double-click Run Shell Script
Type in whatever command you want to run. For example, try the command to toggle Dark Mode:
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode'
File -> Save
Drag the saved file to the Dock, done!
pip install mac-appify
I had trouble with the accepted solution but this command worked for me.
Install
pip install mac-appify
Run
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/appify ~/bin/webex_start.sh ~/Desktop/webex.app
Adding to Cahan's clear answer ... to open a shell script from the dock without passing any arguments to it, try:
open [name of your script].scpt"
example:
open "//Users/user/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/myScript.scpt"
Someone wrote...
I just set all files that end in ".sh" to open with Terminal. It works
fine and you don't have to change the name of each shell script you
want to run.

Resources