Is it possible to open a text file in the current terminal by using xdg-open - shell

I'm wondering if it is possible to open a text file in the current terminal (e.g. with vim) by using xdg-open ?
I'd like to get rid of of my workaround (a custom shell script)
filename="$1"
case "${filename##*.}" in
ods)
open "$filename"
;;
*)
vim "$filename"
;;
esac

Related

Use drag and drop to populate bash scrip for awk [duplicate]

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

Bash Scripting: How to open Kate with directory and filename?

I am completely new to Bash scripting so bare with me while I try to describe this.
What I want to do be able to insert two arguments.
Arg1=Sub Folder in Documents
Arg2=Title of the text file
Using Ubuntu 20.10. (If that matters)
In short: kate ~/Documents/$Arg1/$Arg2 would be the equivalent to the command I would enter into my terminal.
The extra catch is having a keyword shortcut for the Sub Folder. For example say the Sub folder was name SUPERLONGNAME_EXTRALONG but I want a shortcut such as SHORTNAME=SUPERLONGNAME_EXTRALONG
#!/bin/sh
newfile()
dirname=$1
filename=$2
if $dirname==dir1
dirname=newdirname
fi
kate ~/Documents/$dirname/$filename
This is basically what I have now. Although as you would guess this doesn't work. (Provided for aid in seeing what I am trying to do). I can open kate within the home directory with the file name of my choice. My real issue seems to be the creating a shortcut keyword. As well as having the file save to the Document/Arg1 directory. Please help.
I run the command through terminal using
sh newfile arg1 arg2
Use an associate array shortnames to optionally translate from long to short names. Verify that file and directory exist:
#!/bin/bash
declare -A shortnames=(
[long]="short"
)
d=$1
f=$2
if [ -z "$d" ]
then
echo directory required
exit 1
fi
if [ -n "${shortnames[$d]}" ]
then
d=${shortnames[$d]}
fi
d=~/Documents/$d
if [ ! -d "$d" ]
then
echo "directory $d does not exist"
exit 1
fi
if [ -z "$f" ]
then
echo file required
exit 1
fi
kate "$d/$f"

open file if it exists, otherwise, create a new one and open it

I currently have the following alias set up:
alias emacs='open -a Emacs'
But obviously, if the file doesn't exist already, it gives me an error. Is there a way to change this alias to basically say "if the file exists open it in Emacs, otherwise, create the file and then open it in Emacs?"
Thanks!
Use a function instead of an alias. Functions can do everything aliases can do and much more.
emacs() {
if [ ! -f "$1" ]; then
touch "$1"
fi
open -a Emacs "$1"
}
That should work for one file. For multiple files you can use this.
emacs() {
for file; do
if [ ! -f "$file" ]; then
touch "$file"
fi
done
open -a Emacs "$#"
}
You can consider an alias with --args option for open command like this:
alias emacs='open -a Emacs --args= '
Then call it as:
emacs $PWD/file.txt
As per man open:
--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.
Or create a small script called omacs with this:
#!/bin/bash
for f in $*; do if [ -f $f ]; then open -a Emacs $f; else touch $f; open -a Emacs $f; fi; done
And create this alias
alias emacs='omacs'
Works with multiple files.

Create executable bash script that accepts drag & drop

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

Batch localize using IBTools?

Is there a way to run IBTools on a bunch of NIB files with a single command? I'm trying to extract strings from NIBs. Am I supposed to run ibtools once for each NIB?
I find it tedious to run IBTools so many times. (I have only 9 NIB files. It could be worse...)
I don't think ibtool can take multiple files as argument. The only way I see would be to write a bash script to perform this task.
#!/bin/bash
find . -name "*.xib" | while read FILENAME;
do
ibtool --export-strings-file $FILENAME.strings $FILENAME
done
Here is a much more full featured script I made to use for the same operation:
#!/bin/bash
# Argument = -o output_dir -i input_dir
usage()
{
cat << EOF
usage: $0 [options]
This script generates strings files from all xibs in a given directory.
OPTIONS:
-h Show this message
-i Input directory where XIBs are located [./]
-o Output directory where .strings files will be generated
EOF
}
INPUT_DIRECTORY="."
OUTPUT_DIRECTORY="."
while getopts “hi:o:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
i)
INPUT_DIRECTORY=$OPTARG
;;
o)
OUTPUT_DIRECTORY=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $INPUT_DIRECTORY ]] || [[ -z $OUTPUT_DIRECTORY ]]
then
usage
exit 1
fi
# do the generation
find $INPUT_DIRECTORY -name "*.xib" | while read FILENAME;
do
XIBNAME=$(basename "$FILENAME")
XIBNAME="${XIBNAME%.*}"
ibtool --generate-strings-file $OUTPUT_DIRECTORY/$XIBNAME.strings $FILENAME
done

Resources