Nautilus script not showing up in right click menu when nothing is selected - ubuntu-20.04

I have the following script to add an action to my right click menu in Nautilus (using Ubuntu 20.04):
~/.local/share/nautilus/scripts/Open with Disk Usage Analyzer:
#!/usr/bin/env bash
if [ -n "$NAUTILUS_SCRIPT_SELECTED_URIS" ]; then
set $NAUTILUS_SCRIPT_SELECTED_URIS
if [ $# -eq 1 ]; then
destination="$1"
if [ ! -d "$destination" ]; then
destination="$(dirname "$destination")"
fi
else
zenity --error --title="Error - Open with Disk Usage Analyzer" --text="You can only select one directory."
exit 1
fi
else
destination="$NAUTILUS_SCRIPT_CURRENT_URI"
fi
baobab "$destination"
The script appears properly in the right click menu when a folder (or file) is selected:
However, if no files or folders are selected, the Scripts entry does not appear at all in the right click menu:
How do I get the Scripts entry to appear in the right click menu regardless of whether a selection is made or not?

Nautilus has always been like this, I used Pcman-FM, I migrated to Nautilus in 2009, and it has always been that way, unfortunately, you have to select a folder to have the script. It's better to use fff or ranger :)

Related

Open Xcode project and set active file from terminal

Opening an Xcode project from the terminal is easy:
open Foo.xcodeproj/
But that just opens the project and resumes its previous state using UserInterfaceState.xcuserstate - so it just opens to the last active file you were editing.
Is there a way to open an Xcode project and specify which file it should open to?
What I've tried:
Editing .xcuserstate - nightmare, don't do it.
Running open Foo/Foo.xcodeproj/ then open Foo/Sources/main.swift which works some of the time, but not always. (If you just generated the project and do this, it'll open the project, then in a separate window it'll open the file.)
Any other ideas?
An Xcode engineer named Mike pointed me at the loaded property of Xcode's workspace document scripting class. By polling this, we can wait until Xcode has finished loading the project (including loading the editor pane) before asking it to open the file. This lets up reliably open the file in its project's window.
Here's the xopen script I wrote:
#!/bin/bash
shopt -s nullglob
sourceFile="$1"
case "$sourceFile" in
/*) ;;
*) sourceFile="$PWD"/"$sourceFile" ;;
esac
projectDir="$sourceFile"
while [[ $projectDir = */* ]]; do
projectDir="${projectDir%/*}"
candidates=("$projectDir"/*.xcodeproj)
candidate="${candidates[0]}"
if [[ "$candidate" != "" ]]; then
jPath="$candidate"
fi
done
if [[ "$jPath" = "" ]]; then
echo 1>&2 "error: couldn't find .xcodeproj in any parent directory"
exit 1
fi
exec osascript - "$jPath" "$sourceFile" <<EOF
on run argv
set jPath to item 1 of argv
set sourceFile to item 2 of argv
tell app "Xcode"
set wsDoc to (open jPath)
set waitCount to 0
repeat until wsDoc's loaded or waitCount ≥ 20
set waitCount to waitCount + 1
delay 1
end repeat
if wsDoc's loaded then
open sourceFile
end if
end tell
end run
EOF
This script uses the shell to walk up the directory tree from the source file (given as a command line argument) until it finds a directory containing an Xcode project package. Then it passes the path to the project and the path to the source file to an AppleScript. The AppleScript asks Xcode to open the project. If Xcode already has the project open, it'll just bring the existing project window to the front.
Next, the script polls Xcode until it reports that the workspace document is loaded, or until 20 seconds have elapsed.
Finally, if the workspace document is loaded, it asks Xcode to open the source file. Xcode will open the source file in the existing project window's editor.

Applescript to Save File as Two Separate File Formats with Fixed Extensions

I often need to save a screenshot of an art file as two different files. The first would be a PDF with a "_prv" at the end of the file name. The second would be a JPG with an "_thm" file name.
So, examples of file names would be:
1733419_prv.pdf
1733419_thm.jpg
The "1733419" part would be job-specific, and it would be nice if the script would prompt the user the enter that information. It would also be nice if the script could prompt the user for the location that the new files should be saved to.
Is this all possible as an applescript or possibly a Photoshop script?
Thank you so much for any help!
Bryan
Updated Answer
I have improved the script a little. It will now do nothing and exit gracefully if you select Cancel when entering the Job Reference number. It will also now handle spaces in the Job Reference number. It also creates an output directory on your Desktop, so if you enter a Job Reference number of 1234 you will see a directory (folder) called 1234 appear on your Desktop which contains the 2 output files.
#!/bin/bash
# Ask user for job reference, store in $ref
ref=$(osascript -e 'Tell application "System Events" to display dialog "Enter Job Reference:" default answer ""' -e 'text returned of result' 2>/dev/null)
# Quit without doing anything if user didn't enter anything
[ -z "$ref" ] && exit
# Make a place for the output files
DIRECTORY="$HOME/Desktop/$ref"
mkdir "$DIRECTORY" 2> /dev/null # Don't worry if it already exists
cd "$DIRECTORY"
# Get user to select area to grab, then save as JPEG
screencapture -i -t jpg "${ref}_thm.jpg"
# Convert JPEG to PDF
sips -s format pdf "${ref}_thm.jpg" --out "${ref}_prv.pdf" > /dev/null 2>&1
Original Answer
I hope this is close to what you mean. Save it in your HOME directory as grab, then start Terminal and type:
chmod +x grab
which will make the file executable.
#!/bin/bash
# Ask user for job reference, store in $ref
ref=$(osascript -e 'Tell application "System Events" to display dialog "Enter Job Reference:" default answer ""' -e 'text returned of result' 2>/dev/null)
# Get user to click on a window to grab, then save as JPG
screencapture -w -i -t jpg ${ref}_thm.jpg
# Convert JPG to PDF
sips -s format pdf ${ref}_thm.jpg --out ${ref}_prv.pdf > /dev/null 2>&1
You can now either type:
./grab
or double-click on the file called grab in the Finder.
It will ask you for a job reference and then start the screen grab. You should click on a window to tell it which one you mean. It will then create the two files you asked for.
The advantage of this method is that no extra software is required to be installed - such as ImageMagick or anything since it uses built-in OSX tools only.

Open plain text files with LightTable by default when double-clicking them (in ubuntu 14.04 64bits)

I put LightTable in /opt/LightTable
By default, when you install the text editor LightTable in ubuntu 14.04 64 bits, you don't have an "open with LightTable" when you right-click the file you want to open with it.
Therefore I created a file /home/theuser/.local/share/applications/LightTable.desktop containing :
[Desktop Entry]
Name=LighTable Text Editor
Comment=Edit text files
Exec=/opt/LightTable/LightTable %f
Terminal=false
Type=Application
Icon=/opt/LightTable/core/img/lticon.png
Categories=Utility;TextEditor;
StartupNotify=true
MimeType=text/plain
so that a "open with LightTable" appears when I want open a file with LighTable. Now, the problems begin. When I do this, it only opens LightTable, as if I only ran the script
/opt/LightTable/LightTable
Therefore I went to see the script :
#!/bin/bash
BIN=ltbin
HERE=`dirname $(readlink -f $0)`
LIBUDEV_0=libudev.so.0
LIBUDEV_1=libudev.so.1
add_udev_symlinks() {
# 64-bit specific; look for libudev.so.0, and if that can't be
# found link it to libudev.so.1
FOLDERS="/lib64 /lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib /lib"
for folder in $FOLDERS; do
if [ -f "${folder}/${LIBUDEV_0}" ]; then
return 0
fi
done
for folder in $FOLDERS; do
if [ -f "${folder}/${LIBUDEV_1}" ]; then
ln -snf "${folder}/${LIBUDEV_1}" "${HERE}/${LIBUDEV_0}"
return 0
fi
done
echo "${LIBUDEV_0} or ${LIBUDEV_1} not found in any of ${FOLDERS}".
exit 1
}
add_udev_symlinks
ARGS="$#"
CORRECTED=${ARGS//-/<d>}
CORRECTED=${CORRECTED// /<s>}
if [ -t 0 ] && [ $# != 0 ]; then
#We're in a terminal...
LD_LIBRARY_PATH="$HERE:$LD_LIBRARY_PATH" $HERE/$BIN "<d><d>dir=`pwd`<s>$CORRECTED" &
else
#We were double clicked
LD_LIBRARY_PATH="$HERE:$LD_LIBRARY_PATH" $HERE/$BIN &
fi
and replaced its ending if/else/fi by a simple :
LD_LIBRARY_PATH="$HERE:$LD_LIBRARY_PATH" $HERE/$BIN "<d><d>dir=`pwd`<s>$CORRECTED" &
so that the right behaviour (the one when opening a file from the terminal) is chosen.
This almost made my day. Now, if I double-click a file or right-click it and select "open with LightTable", the file is indeed opened in LightTable... but : this is true only if the file name and the path to the file have no blank space in their names.
If the file named "the file" is in the path thepath without space in it, when I double-click it, it opens two blank files "the" and "file" in LightTable. The same behavior is constated if thepath has space(s) in it.
Would someone have an idea ? I guess I should correct the bash script, but I'm not an expert in it. (I am not even sure that the script is really wrong...)
Thanks in advance
MEF
Your question has a tl;dr quality about it.
When you store "$#" in a variable, you really have to use an array and lots of quotes to preserve the elements with whitespace:
ARGS=("$#")
CORRECTED=("${ARGS[#]//-/<d>}")
CORRECTED=("${CORRECTED[#]// /<s>}")
But then the way you have to pass the args is a problem:
LD_LIBRARY_PATH="$HERE:$LD_LIBRARY_PATH" $HERE/$BIN "<d><d>dir=`pwd`<s>$CORRECTED" &
It's impossible to expand the array into a single space-delimited string and then somehow extract the elements that have significant whitespace.
You may have to do this, and see if it works:
export LD_LIBRARY_PATH="$HERE:$LD_LIBRARY_PATH" # might as well put on own line
"$HERE/$BIN" "<d><d>dir=`pwd`<s>" "${CORRECTED[#]}" &
# ...........^^^^^^^^^^^^^^^^^^^^ standalone argument
Actually, this is a bug in LightTable.
I opened an issue (https://github.com/LightTable/LightTable/issues/1762) and submitted a patch (https://github.com/LightTable/LightTable/pull/1763) to fix this :
There are 2 issues here:
currently the deployed Bash script doesn't pass any arguments to LightTable if it's not invoked from a terminal, but this is needed e.g. to make a gnome desktop shortcut. This issue can also be reproduced by using the ALT+F2 launcher under Ubuntu.
independently LightTable cannot currently open files whose names contain ' ' characters.

dialog menu to disply files and select one of them

Please advise me on how I can do the following with the dialog utility:
I want to display all the files under /home directory in menu, and select only one of them. Then the script will print the full path of the selected file.
I have created the following script:
this script only displays the files in the dialog box menu
#!/bin/bash
dialog --title "List file of directory /home" --msgbox "$(ls /home )" 100 100
Code & copy/paste not really available on the stack exchange android app.
CHOICE='dialog --title "List " --radiolist "$(ls /home)" 100 100'
echo "/home/$CHOICE"
Note: i have used ' for back-tick my keyboard has no back-tick.
Firstly change --msgbox to say --radiolist. Enclose the whole of the dialog command in back-ticks and precede the whole lot CHOICE=.
It'll now read
CHOICE='dialog...
Next line reads
echo "$$HOME/$CHOICE"
Only type one $ before HOME, i either have no dollar signs or this.

Windows equivalent of the Mac OS X “open” command

Liu Chang asked a very similar question to this one here, Linux equivalent of the Mac OS X "open" command.
Is there a windows equivalent for the Mac OS X "open" command. I'm trying to run a profiler that will open it's results, but it's looking for the "open" command. Basically, the command needs to open a file from the command prompt as if it were double-clicked on in explorer.
The closest thing available is start.
If its first argument is double-quoted, that argument is treated as a window title rather than a filename. Thus, to use it robustly, add an empty string as the first argument:
start "" "my filename.foo"
Thank you to #Holger for pointing this out!
I use to write
explorer.exe <file>
Just typing the file name into a console window will open the file in Windows. I tried several formats - .doc opened with OpenOffice, .mp3 opened with Windows Media Player, and .txt opened with Wordpad. This is the same behavior I experience when double clicking on the files.
Try explorer <filename>. For example I wanted to launch a folder named abc placed at desktop, so I used the command
explorer abc
Use start
I am using Windows 10 and:
> start .
Opens the current directory in File Explorer
> start text.txt
Opened text file in Notepad++ (It is set as my default Txt editor)
I am answering this again as the accepted answer is not correct which
is using the start command (which opens up the CMD as a new instance) and also because The Equivalent according
to me is explorer.exe as also mentioned by others but not clarified
as it should have been!
So, If you want to open the current folder like that with the 'open' command. you should use
explorer.exe .
which will open the current folder in explorer or if you just do the
explorer.exe
then you will open the default This PC location (whether it is recents or my computer or anything else)
It just works as in, when you just type in the file name it will just open in the default program just like
somevideo.mp4
and if you want that file/video to open/play with some other program just write down the program name with the full path (if it's not in the PATH system variable) followed by the filename like
"C:\program files\greentree applications\vlc.exe" somevideo.mp4
Only explorer.exe appears to work under cygwin.
If you use cygwin (or git bash), here's a quick script hack. Change the EDITOR to be whatever you want:
#!/bin/sh
# open
EDITOR="exec subl.exe"
BROWSER="/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"
if [ -d "$1" ]; then
exec explorer.exe $(cygpath -w "$1")
elif [ -f "$1" ]; then
path=$(cygpath --windows "$1")
case "$1" in
*.xml) $EDITOR "$1";;
*.txt) $EDITOR "$1";;
*.html) "$BROWSER" "$path";;
file://*) "$BROWSER" "$path";;
http://*) "$BROWSER" "$path";;
https://*) "$BROWSER" "$path";;
esac
else
# TODO non-existent file/dir
echo "non existent file: $1"
exit 1
fi
exit 0
'start' is definitely the closest thing for Windows as #charles-duffy stated. Depending on your project there are also a few tools out there that solve this problem.
Node opn is a pretty great solution to be totally cross platform

Resources