The officially official Devuan Forum!

You are not logged in.

#1 2021-03-18 21:14:14

greenjeans
Member
Registered: 2017-04-07
Posts: 541  
Website

[SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Hey guys, been out of touch for a long time now, big moves, covid, etc. been getting in the way of my linux'ing...anyhoo, had a question about some extensions i've been using and working on again and need some help if anybody has time. I'm still a terrible rookie coder, lol, trying to leanr more though.

Here's the main one, it's a right-click context menu extension for pcmanfm that re-sizes pics using imagemagick for the backend and yad, super-handy and fast, and works perfectly...except...spaces in file or folder names break it, and can't seem to fix it with usual methods. I can finagle it into working with spaces in filenames but then it drops all output into home folder instead of current directory and no amount of cd or mv commands seems to make it work right. I'm pretty good about not putting spaces in file/folder names, but ya know, would be nice if it didn't break script.

Here's the code, a .desktop file that goes into /home/user/.local/share/file-manager/actions and the script I made (usr/bin):

[Desktop Entry]
Type=Action
Name=Resize Image
Comment=Shrink image and convert to jpg
Icon=edit-cut
Profiles=resize;

[X-Action-Profile resize]
Exec=sh -c 'cd %d && shrink2 %f'
MimeTypes=image/bmp;image/jpeg;image/png;
#!/bin/sh

sizebox=$(yad --fixed --window-icon=edit-cut --form --title="Resize Image" --field="Resize to:" --width=400 --text-align=center --text="Enter new size (W x H in pixels, i.e. 800x600, 1024x768 etc.)")

size=$(echo $sizebox | awk 'BEGIN {FS="|" } { print $1 }')

convert "$1" -resize "$size" -set filename:copy '%t-%wx%h' '%[filename:copy].jpg'

# Depends: yad, imagemagick
# (note: later versions of imagemagick may require a different command than 'convert')

# DESCRIPTION:
# This script makes a copy of an image file (JPG, BMP, PNG), resizes the copy to whatever
# dimensions are entered, converts it to a JPEG, adds the new dimensions to the original
# filename, and outputs the copy into the same directory as the original. It is intended to
# be used with a .desktop file for activation in PcmanFM to offer a "Resize Image"
# option when you right-click an image file, but can also be used in terminal: shrink2 [img file]

Thanks!
~greenjeans


https://sourceforge.net/projects/vuu-do/
Vuu-do GNU/Linux, minimal Devuan-based openbox systems to build on, maximal versions if you prefer your linux fully-loaded.

Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Offline

#2 2021-03-18 21:35:49

golinux
Administrator
Registered: 2016-11-25
Posts: 3,316  

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Welcome home, greenjeans!!  big_smile

Offline

#3 2021-03-18 23:18:00

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,251  

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Perhaps double quotes around %f makes a difference? and around %d as well, probably.

Offline

#4 2021-03-19 11:29:41

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

why is their a cd in the exec line of the .desktop file ?

Im pretty sure double quotes would rectify this as ralph mentions.

[Desktop Entry]
Type=Action
Name=Resize Image
Comment=Shrink image and convert to jpg
Icon=edit-cut
Profiles=resize;

[X-Action-Profile resize]
Exec=sh -c 'cd "%d" && shrink2 "%f"'
MimeTypes=image/bmp;image/jpeg;image/png;

confirmed working with gaps in file name, im using nemo file manager though.

Last edited by dice (2021-03-19 11:35:18)

Offline

#5 2021-03-19 20:30:56

greenjeans
Member
Registered: 2017-04-07
Posts: 541  
Website

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Thanks y'all!

First thing I tried was all kinds of quotes around the %f and %d and didn't work for me, but will go back and try again to be sure, normally that works in terminal but seems like it wouldn't work here because the entire command string is quoted.

@dice: cd is to first change the directory to current one, or it will drop all output into the home folder regardless of what directory you're in.

Okay, just tried the recommended double quotes again, still no joy.

@Golinux: Hi there Lady! Hope all is well down there, FYI we may be moving back home soon, so maybe i'll get to see ya! ;-)


https://sourceforge.net/projects/vuu-do/
Vuu-do GNU/Linux, minimal Devuan-based openbox systems to build on, maximal versions if you prefer your linux fully-loaded.

Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Offline

#6 2021-03-19 20:45:25

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Try using "$@" instead of "$1" in the convert line of the shell script.

Off topic but I would also suggest using whiptail (in a terminal) rather than yad because the output could be parsed without resorting to awk:

https://en.wikibooks.org/wiki/Bash_Shel … #Input_box


Brianna Ghey — Rest In Power

Offline

#7 2021-03-19 22:00:50

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,251  

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

There's also the possibillity that the filenames are "urlencoded" by the file manager. It would mean that spaces have been replaced with "%20" (and a bit more).

You might try adding the following function:

urldecode() {
    bash -c "printf '%b' '$(echo "$1" | sed 's|%%| |g;s|%|\\x|g;s| |%|g')'"
}

to your /bin/sh script, and then use

convert "$(urldecode $1)" ...

TL;DR; In detail, that urldecode function for dash processes its given argument with sed to replace all %NN with \xNN except for occurrences  of %% which instead are replaced with single %. The resulting string is printed with bash printf using the %b format which makes all \xNN into characters of hexadecimal ASCII code NN.

Note that the sed processing uses space temporarily to make the %%-to-% translation bypass the  %NN-to-\xNN translation. It therefore misbehaves for an input that already has space characters.

Offline

#8 2021-03-20 19:48:24

greenjeans
Member
Registered: 2017-04-07
Posts: 541  
Website

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Okay so, even stranger....

Last night when I got home and started messing with it, I decided to double-check whether quoting changes would help, and I must never have tried replacing the single-quoted string in the .desktop with double quotes, and lo and behold it fixed it!  but....

1. I don't know why that would fix it and would really like to understand why...no biggie but....

2. I have an almost identical extension that works the same way only it provides a "rotate image" function instead of re-sizing, so after fixing the first one I thought YAY this should fix the other too...but it didn't, still fails on spaces in file/folder names...what the actual heck???

Here's the new re-size .desktop and it works perfectly, zero issues now:

[Desktop Entry]
Type=Action
Name=Resize Image
Comment=Shrink image and convert to jpg
Icon=edit-cut
Profiles=resize;

[X-Action-Profile resize]
Exec=sh -c "cd %d && shrink2 %f"
MimeTypes=image/bmp;image/jpeg;image/png;

And here's the .desktop for the rotater,did the same thing, changed the single quotes to double quotes, again the only change I made:

[Desktop Entry]
Type=Action
Name=Rotate Image
Comment=Make rotated copy of image
Icon=edit-redo
Profiles=rot;

[X-Action-Profile rot]
Exec=sh -c "cd %d && rot %f"
MimeTypes=image/bmp;image/jpeg;image/png;

As you can see, pretty much identical...dunno why it won't work here when it did on the other...here's the script for the rotater:

#!/bin/sh

rotbox=$(yad --fixed --window-icon=edit-redo --form --title="Rotate Image" --field="Rotate:":CB --width=300 --text-align=center --text="Select rotation (clockwise, in degrees)" '90!180!270')

rot=$(echo $rotbox | awk 'BEGIN {FS="|" } { print $1 }')

convert $1 -strip -rotate "$rot" -set filename:copy '%t-rotated.%e' '%[filename:copy]'

# Depends: yad, imagemagick
# (note: later versions of imagemagick may require a different command than 'convert')

# DESCRIPTION:
# This script makes a copy of an image file (JPG, BMP, PNG), rotates the copy to
# selection entered in yad dialog (90, 180 or 270 degrees clockwise), adds "rotated" to 
# the original filename, and outputs the copy into the same directory as the original. 
# It is intended to be used with a .desktop file for activation in PcmanFM to offer a "Rotate Image"
# option when you right-click an image file, but can also be used in terminal: rot [img file]

https://sourceforge.net/projects/vuu-do/
Vuu-do GNU/Linux, minimal Devuan-based openbox systems to build on, maximal versions if you prefer your linux fully-loaded.

Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Offline

#9 2021-03-20 20:05:11

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Have you tried changing this line:

greenjeans wrote:
convert $1 -strip -rotate "$rot" -set filename:copy '%t-rotated.%e' '%[filename:copy]'

To this:

convert "$@" -strip -rotate "$rot" -set filename:copy '%t-rotated.%e' '%[filename:copy]'

If the file name is separated by spaces then it will be interpreted as multiple arguments for the script and "$@" will pass them all through whereas $1 (or "$1", as it should be) will only pass the first part of the file name, which won't be recognised.


Brianna Ghey — Rest In Power

Offline

#10 2021-03-20 20:37:07

greenjeans
Member
Registered: 2017-04-07
Posts: 541  
Website

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Head_on_a_Stick wrote:

Have you tried changing this line:

greenjeans wrote:
convert $1 -strip -rotate "$rot" -set filename:copy '%t-rotated.%e' '%[filename:copy]'

To this:

convert "$@" -strip -rotate "$rot" -set filename:copy '%t-rotated.%e' '%[filename:copy]'

If the file name is separated by spaces then it will be interpreted as multiple arguments for the script and "$@" will pass them all through whereas $1 (or "$1", as it should be) will only pass the first part of the file name, which won't be recognised.

Wow, good eye, I never noticed that I hadn't quoted the $1 in the second script...huh, but still worked other than the spaces issue...I made these like 4 years ago and was in a hurry to get 'em working, I sell a lot of stuff online and the re-size option is a huge time-saver for me, and the rotater is nice for the occasional cell-phone pic that needs to be rotated.

Okay, trying stuff now, lol, BRB.


https://sourceforge.net/projects/vuu-do/
Vuu-do GNU/Linux, minimal Devuan-based openbox systems to build on, maximal versions if you prefer your linux fully-loaded.

Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Offline

#11 2021-03-20 20:44:26

greenjeans
Member
Registered: 2017-04-07
Posts: 541  
Website

Re: [SOLVED] Need a lil coding help with an extension (shell script), openbox/pcman

Man i'm glad I posted these things...we don't have internet at the house, using the library wi-fi now that they finally opened back up, so for months all i've had to work with are man pages, and nobody to ask to look at something for me, it's amazing how a fresh set of eyes can spot so quickly little mistakes that have been staring me in the face for days, lol.

Thanks Head_on_a_stick, all that was needed on that second script was to double-quote the $1 (plus the change to the .desktop) and the whole thing works perfectly now!

You guys rock!


https://sourceforge.net/projects/vuu-do/
Vuu-do GNU/Linux, minimal Devuan-based openbox systems to build on, maximal versions if you prefer your linux fully-loaded.

Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Offline

Board footer