I'd like to add a right click menu item to easily resize images in files.
I found that the following script does what I want when I run it in a Terminal:
convert myimage.jpg -resize '1200x1200' -set filename:fname '%t-1200px.%e' +adjoin '%[filename:fname]'
But when add a resize-image.contract
file to ~/.local/share/contractor
with the following content, nothing seems to happen.
[Contractor Entry]
Name=Resize to 1200px
MimeType=image;
Exec=convert %U -resize '1200x1200' -set filename:fname '%t-1200px.%e' +adjoin '%[filename:fname]'
Gettext-Domain=pantheon-terminal
(This scripts requires imagemagick
)
Is there a way to debug this? When I run pantheon-files --debug
I don't see relevant output.
Issue on Contracts documentation: https://github.com/elementary/contractor/issues/16
Update: I've tried splitting up the script, like Jeremy Wootten suggested, see below, but that doesn't seem to help.
~/.local/share/contractor/resize-image-1200.contract:
[Contractor Entry]
Name=Resize to 1200px
MimeType=image;
Exec=convert %U -resize '1200x1200' -set filename:fname '%t-1200px.%e' +adjoin '%[filename:fname]'
Exec=/usr/local/bin/resize-image-1200.sh %U
Gettext-Domain=pantheon-terminal
/usr/local/bin/resize-image-1200.sh:
#!/bin/bash
inputfile=${1?'please supply input file name'}
echo "input: $inputfile"
convert $inputfile -resize '1200x1200' -set filename:fname '%t-1200px.%e' +adjoin '%[filename:fname]'
Update 2: With the tips from Jeremy Wootten I have something that kind of works, except that it doesn't use convert's rename trick, so now it's limited to jpg files.
~/.local/share/contractor/resize-image-1200.contract
:
[Contractor Entry]
Name=Resize to 1200px
MimeType=image;
Exec=resize-image-1200.sh %U
Gettext-Domain=pantheon-terminal
/usr/local/bin/resize-image-1200.sh
:
#!/bin/bash
convert "$1" -resize '1200x1200' "$1-test.jpg"
I've opened an issue on debugging .contract files: https://github.com/elementary/contractor/issues/20
Update 3:
After a tip on debugging I changed my /usr/local/bin/resize-image-1200.sh
script to:
#!/bin/bash
exec 2> /tmp/resize-image-1200.sh.log
exec 1>&2
set -x
convert "$1" -resize '1200x1200' -set filename:fname '%t-1200px.%e' '%[filename:fname]'
This created /tmp/resize-image-1200.sh.log
with:
+ convert '/home/peteruithoven/NextCloud/InstantUpload/OpenScope/thunar test/IMG_20171116_003459.jpg' -resize 1200x1200 -set filename:fname %t-1200px.%e '%[filename:fname]'
Which indicates that it ran the script successfully. Running this myself I found out it would put the resulting resized image file in the folder I was executing the script from, so I think that's the next challenge.
Update 4: Instead of using Imagemagick to rename I'm now using bash functionality.
/usr/local/bin/resize-image-1200.sh
:
#!/bin/bash
exec 2> /tmp/resize-image-1200.sh.log
exec 1>&2
set -x
DIR=$(dirname "$1")
BASENAME=$(basename "$1")
FILENAME=${BASENAME%.*}
EXT=${BASENAME##*.}
convert "$1" -resize '1200x1200' +adjoin "$DIR/$FILENAME-1200px.$EXT"
One downside is that this script wouldn't work with multiple files, but then again, even when selecting multiple files and running the contract the .sh script only gets the path to the first file.