There is this solved topic about opening .html files via command line.
I use the solution and it works well for using
open ./myfile.html
However, it opens the file always in a new tab. I would like to open it always in the same tab (using the browser target). This is an easy thing to do in JavaScript, but I can't figure out a way to do it in combination with the above mentioned code.
My assumption for now is, that there must be a way to pass the target as a parameter to the open command. The man open reveals the following for the parameter --args:
All remaining arguments are passed to the opened application in the
argv parameter to main(). These arguments are not opened or
interpreted by the open tool.
So I tried the following:
open ./myfile.html --args target=myfile_target # still opens in new tab
open ./myfile.html --args target="myfile_target" # still opens in new tab
open ./myfile.html --args target:myfile_target # still opens in new tab
I am not sure if this even works but I think there must be a way to do this.
Edit: for now it is enough to make this work with chrome.
This Bash script incorporates a bit of AppleScript in order to open a browser window with a reference that the script can keep track of and continue to target with ongoing URL requests.
You ought to be able to copy-n-paste it into a text editor and save it as whatever you wish to call this replacement open function. I saved it as url, in one of the directories listed in my $PATH variable. That way, I could simply type url dropbox.com from the command-line, and it would run.
You will have to make it executable before you can do that. So, after it's saved, run this command:
chmod +x /path/to/file
Then you should be good to go. Let me know if you encounter any errors, and I'll fix them.
#!/bin/bash
#
# Usage: url %file% | %url%
#
# %file%: relative or absolute POSIX path to a local .html file
# %url%: [http[s]://]domain[/path/etc...]
IFS=''
# Determine whether argument is file or web address
[[ -f "$1" ]] && \
_URL=file://$( cd "$( dirname "$1" )"; pwd )/$( basename "$1" ) || \
{ [[ $1 == http* ]] && _URL=$1 || _URL=http://$1; };
# Read the value on the last line of this script
_W=$( tail -n 1 "$0" )
# Open a Safari window and store its AppleScript object id reference
_W=$( osascript \
-e "use S : app \"safari\"" \
-e "try" \
-e " set S's window id $_W's document's url to \"$_URL\"" \
-e " return $_W" \
-e "on error" \
-e " S's (make new document with properties {url:\"$_URL\"})" \
-e " return id of S's front window" \
-e "end try" )
_THIS=$( sed \$d "$0" ) # All but the last line of this script
echo "$_THIS" > "$0" # Overwrite this file
echo -n "$_W" >> "$0" # Appened the object id value as final line
exit
2934
Related
I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?
I wanted to make it look something like this:
mplayer <get full path to file.file-ending>
I want the executable bash file to sit on my desktop ;)
If possible, I'd just like an rightclick -> start with mplayer function, but I don't know how to make one.
You can access arguments passed to the script with $1 (for the first argument). And also you should make a .desktop file so Nautilus (or your desktop manager) know what to do and use %u to pass the dropped path to the script.
For example you can create a file named DropOverMe.desktop:
[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application
I use gnome-terminal as I have Ubuntu on my PC, use your preferred terminal application.
And the script could be something like:
#! /bin/bash
echo "Launched with $1" >> /tmp/history.log
Try:
#!/bin/bash
mplayer "$1"
The file path of the dropped file will be passed to the script file as the 1th command line argument.
In openned terminal
By using mate-terminal, gnome-terminal or konsole, you could use drag'n drop into oppened window.
This will send URL as tipped, with a space added, but without endline.
For this, run mplayer, I wrote this little loop function:
while IFS=$' \t\r\n' read -d '' -p "Wait for URL to play: " -rsn 1 str &&
[ "$str" ];do
while IFS= read -d '' -rsn 1 -t .02 char
do str+="$char"
done
if [ "$str" ] ;then
read -a req <<<"$str"
echo $req
mplayer $req
fi
done
First read will determine if something is comming or else end loop.
Second loop with very short timeout will read dropped URL as a single string
read -a req will split string to consider only 1st part
I made a simple custom script to quick open a project on vscode thanks to ghq:
selection=$(ghq list | rofi -dmenu -p "Project" -no-custom)
[[ ! -z "${selection}" ]] && code ${HOME}/p/${selection}
exit 0
I bound it on i3 config:
bindsym $mod+d exec --no-startup-id rofi -show combi
bindsym $mod+p exec --no-startup-id ~/.config/rofi/project.sh
It works like a charm. Howeverm I want to make it part of the combi mode in order to have only one shortcut.
How can I achieve this?
If this is not possible with rofi, does a similar tool permit that?
It is possible to do by adding a script to your rofi config in ~/.config/rofi/config.rasi:
configuration {
combi-modi: "window,drun,ssh,Project:~/.config/rofi/project.sh";
modi: "combi";
}
Then you need to modify your script, so it will return a list of projects when no arguments are given and open a project when the script is launched with a project name as an argument:
#!/usr/bin/env sh
# List projects if no arguments given
if [ -z "$1" ]; then
ghq list
else
code "$HOME/p/$1"
fi
Now, rofi -show combi should open with a default combi mode+project launcher.
By the way, I am not sure if the exit 0 at the end of the script is needed. It makes the script always return successful status, no matter the exit status of used commands. See the answer here.
In my case, I was trying a simple file finder script for rofi
At first my attempt was:
fd -H . ~ | rofi -dmenu | xargs xdg-open > /dev/null 2>&1 &
Calling the script from the command line works like a charm, but when I tried this inside rofi, didn't work. Of course I can't call rofi indide rofi
Following the idea here, given by Cezary,I got to this:
#!/bin/bash
if [[ -z "$1" ]]; then
fd -H . ~
else
xdg-open "$1" > /dev/null 2>&1 &
fi
Turns out, de > /dev/null 2>&1 & was important, so it wouldn't freeze the whole thing. Maybe only the "&" in the end is important, so the process goes to the background? The rest, as I understand, just throws away any output or errors.
In my "config.rasi" from rofi, I have the line
modi: "drun,Finder:/path/to/script";
since I want it as a second mode, instead of a combi. Of course I made it executable with chmod +x.
impossible to find an answer to that:
i would like to create a log history of my command line automatically without having to do anything.
For that i found some clues, i modified my .bash_profile
but i need to exclude some command that i don't want in my log like "ls, cd, etc."
this doesn't work, and i can't d
so here's my code:
# log every command typed and when
command_out=( "more" "less" "cd" "open" "ls" "pwd" "nano" "man" "help") #array of command i don't want to save in my log
my_TEST=0 ##setup a var
FIRST_COMMAND=$(echo $BASH_COMMAND| cut -d' ' -f 1) ##get only the first command
## test if the first command is in the array
for elm in "${command_out[#]}"; do
if [[ $FIRST_COMMAND == $elm ]]; then
echo $elm # does not work
$my_TEST=1 ## if the command is in the array the var is setup to 1
fi
done
if [[ $my_TEST == 0 ]] && [ -n "${BASH_VERSION}" ]; then
trap "caller >/dev/null || \
printf '%s\\n' \"\$(date '+%Y-%m-%dT%H:%M:%S%z')\
\$(tty) \${BASH_COMMAND}\" 2>/dev/null >>~/.command_log" DEBUG
fi
if you any other ideas of how to do what i want i'm open
Thanks you
Bash automatically keeps a history of every command you type; you can use the history command to view it. If you want to exclude certain commands, rather than trying to exclude them from the log, I would skip them when viewing it, e.g. history | egrep -vw 'ls|cd|other|commands|here'.
You can set HISTTIMEFORMAT to get a timestamp with every entry, control how many commands are kept with HISTFILESIZE, and if you really want to keep some commands out instead of just not seeing them when you look, you can list them in HISTIGNORE. See https://www.gnu.org/software/bash/manual/html_node/Using-History-Interactively.html.
Your ~/.bash_history file should already contain your complete command history. You could use something like
cat ~/.bash_history | grep -v cd | egrep -v 'cd|ls|...'
to filter out the commands you're not interested in.
So for the list you specified:
cat ~/.bash_history | egrep -v 'more|less|cd|open|ls|pwd|nano|man|help'
I completed Mark Reed answer with what i precisely wanted. Here is my code:
# changes the .bash_history file mode to append
shopt -s histappend
#configures the history -a command to be run at each shell prompt. The -a immediately writes the current/new lines to the history file.
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
#list of command i don't want in my history
HISTIGNORE='ls*:exit:pwd:clear:cd*:man*:more*:less*:head*:tail*:nano*:open*:help*'
#set no limit to the history file size
HISTSIZE= HISTFILESIZE=
Basically i need to have a script that checks if there is a screen running as "serverman".
If it exists, then resume it, otherwise create new.
So i wanted to parse "sceen -ls" and check if there is one called "serverman".
This is "screen -ls" output
There are screens on:
7423.212 (03/09/14 08:48:58) (Detached)
7411.1as (03/09/14 08:48:49) (Detached)
2 Sockets in /var/run/screen/S-root.
I was tinking to a command that could parse it trough piping and search for "serverman" word.
And if any set isalreadyrunning=1, else set it to 0, but i don't know what command to use and how to use, do you?
You can already do this with existing screen options without explicitly checking for a named session. screen -D -R serverman will attach to serverman if exists, but will create it and attach to it if not.
I paste a working script for tmux, as apparently I have old screen on my old MacBook that gives different output. You should be able to modify it easily (but I would advice to switch to tmux, it's way better)
# created sessions with
# tmux new -s hello
# tmux new -s world
SEARCHING="hello"
found_session=$(tmux ls 2> /dev/null | perl -ne "if (/(^${SEARCHING}):/) {print \"\$1\"}" | head -n1)
if [ -z "$found_session" ]; then
echo Session not found, creating new.
tmux new -s $SEARCHING
else
echo Found $found_session, attaching.
tmux a -t $found_session
fi
I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?
I wanted to make it look something like this:
mplayer <get full path to file.file-ending>
I want the executable bash file to sit on my desktop ;)
If possible, I'd just like an rightclick -> start with mplayer function, but I don't know how to make one.
You can access arguments passed to the script with $1 (for the first argument). And also you should make a .desktop file so Nautilus (or your desktop manager) know what to do and use %u to pass the dropped path to the script.
For example you can create a file named DropOverMe.desktop:
[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application
I use gnome-terminal as I have Ubuntu on my PC, use your preferred terminal application.
And the script could be something like:
#! /bin/bash
echo "Launched with $1" >> /tmp/history.log
Try:
#!/bin/bash
mplayer "$1"
The file path of the dropped file will be passed to the script file as the 1th command line argument.
In openned terminal
By using mate-terminal, gnome-terminal or konsole, you could use drag'n drop into oppened window.
This will send URL as tipped, with a space added, but without endline.
For this, run mplayer, I wrote this little loop function:
while IFS=$' \t\r\n' read -d '' -p "Wait for URL to play: " -rsn 1 str &&
[ "$str" ];do
while IFS= read -d '' -rsn 1 -t .02 char
do str+="$char"
done
if [ "$str" ] ;then
read -a req <<<"$str"
echo $req
mplayer $req
fi
done
First read will determine if something is comming or else end loop.
Second loop with very short timeout will read dropped URL as a single string
read -a req will split string to consider only 1st part