This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Migrating a Renaming Nautilus Script to a KDE Service Menu

Tags: None
(comma "," separated)
thyeum
Registered Member
Posts
8
Karma
0
OS
Hi,

I have recently started using Neon after the abandoning of Unity and need help with Service Menus.
In Unity I had the following script to rename and tidy up pictures:
Code: Select all
#!/bin/bash
print ()
{
read -p "Prefix? " prefix             # ask for picture prefix
rename -v 's/\.jpe?g$/.jpg/gi' *          # change the extension of all image files (.JPG .JPEG .jpeg .jpg) to a lower case .jpg extension
jhead -n *.jpg                   # rename all pictures with their timestamp
jhead -n%y%m%d$prefix%03i *.jpg          # rename all pictures with date|prefix|number pattern
exiftool -UserComment= -if '$make eq "samsung"' *.jpg    # delete comment field in pictures taken with Samsung camera (ex: Galaxy Note)
jhead -di *.jpg                # delete the IPTC section
jhead -dx *.jpg                # delete the XMP section
jhead -ft *.jpg                # set the file's system time stamp to what is stored in the Exif header
jhead -dsft *.jpg               # set the Exif timestamp to the file's timestamp
exiftool -delete_original! *.jpg *.JPG *.jpeg      # delete the .original backup files that were automatically created
rm *.jpg_original *.JPG_original *.jpeg_original   # remove any additional .original files that weren't removed with the previous command
}
export -f print
gnome-terminal --execute bash -c "print ; bash"

I am trying to set up the same thing as a service menu, but am struggling mainly because the files get renamed and the commands don't work anymore after the renaming.
So far I've been able to reproduce it using 4 different Actions, but it's a pain to have to click so many times. Any help solving this by having just one Action would be very much appreciated.

Code: Select all
[Desktop Entry]
Type=Service
Name=PicRename
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/jpeg;
Actions=ChangeExtension;DateRename;PrefixRename;PrefixRenameFor;StripDataAndSetTime
Icon=folder-pictures-symbolic
X-KDE-Priority=TopLevel
X-KDE-Submenu=Picture Renaming

[Desktop Action ChangeExtension]
Name=Change Extension
Icon=edit-rename
Exec=count=0; for f in %F; do rename -v 's/\.jpe?g$/jpg/gi' "$f"; count=`expr $count + 1`; done; kdialog --title "Extensions Changed" --passivepopup "$count image(s) with extensions changed!";

[Desktop Action DateRename]
Name=Rename according to date
Icon=edit-rename
Exec=count=0; for f in %F; do jhead -n "$f"; count=`expr $count + 1`; done; kdialog --title "Date Renamed" --passivepopup "$count image(s) renamed according to their date!";

[Desktop Action PrefixRename]
Name=Rename according to prefix
Icon=edit-rename
Exec=prefix=`kdialog --title "Input dialog" --inputbox "Picture prefix?"`; jhead -n%%y%%m%%d$prefix%%03i %F;

[Desktop Action PrefixRenameFor]
Name=Rename according to prefix For Loop
Icon=edit-rename
Exec=prefix=`kdialog --title "Input dialog" --inputbox "Picture prefix?"`; count=001; for f in %F; do jhead -n%%y%%m%%d$prefix$count $f; count=`expr $count + 001`; count=$(printf "%03d" $count); done;

[Desktop Action StripDataAndSetTime]
Name=Strip Data and Set Timestamps
Icon=edit-rename
Exec=count=0; for f in %F; do exiftool -UserComment= -if '$make eq "samsung"' $f; jhead -di $f; jhead -dx $f; jhead -ft $f; jhead -dsft $f; count=`expr $count + 1`; done; rm *.jpg_original *.JPG_original *.jpeg_original; kdialog --title "Data Stripped and Timestamps Set" --passivepopup "$count image(s) ready!";
wolfi323
Registered Member
Posts
1129
Karma
11
OS
thyeum wrote:I am trying to set up the same thing as a service menu, but am struggling mainly because the files get renamed and the commands don't work anymore after the renaming.
So far I've been able to reproduce it using 4 different Actions, but it's a pain to have to click so many times. Any help solving this by having just one Action would be very much appreciated.

Put your commands into a shell script and call this in the Exec line... ;)
wolfi323
Registered Member
Posts
1129
Karma
11
OS
PS: seems I have not completely understood your problem.
Getting the new file name and passing it to the followup commands is probably not easily doable.
You could apply the same regexp that you use in "rename" the actual file name via sed e.g. (or do the rename as the last step), but the renaming via jhead might be more problematic. (Maybe jhead can output the new file name to stdout? Then you'd just need to assign the output to a shell variable to get the new name)

But, for batch renaming files, have a look at krename.
It's normally interactive, i.e. you select the files and how you want to rename them in the GUI, but that can be passed as command line options too to make it "automatic".
thyeum
Registered Member
Posts
8
Karma
0
OS
Thanks for your ideas.
I tried krename, but while the GUI is very powerful, the command line options are very scarce and did not work here.

wolfi323 wrote:Put your commands into a shell script and call this in the Exec line... ;)

I had tried this and couldn't get it to work, but after tinkering a little bit I got it to work. If anyone is interested here is my final service menu/script:
Code: Select all
[Desktop Entry]
Type=Service
Name=PicRename
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=image/jpeg;inode/directory;
Actions=ExecuteScript;
Icon=folder-pictures-symbolic
X-KDE-Priority=TopLevel

[Desktop Action ExecuteScript]
Name=Rename Pictures
Icon=edit-rename
Exec=[script folder]/rename-pics.sh %U

and
Code: Select all
#!/bin/bash
prefix=`kdialog --title "Input dialog" --inputbox "Picture prefix?"`;             # ask for picture prefix
rename -v 's/\.jpe?g$/.jpg/gi' *          # change the extension of all image files (.JPG .JPEG .jpeg .jpg) to a lower case .jpg extension
jhead -n *.jpg                   # rename all pictures with their timestamp
jhead -n%y%m%d$prefix%03i *.jpg          # rename all pictures with date|prefix|number pattern
exiftool -UserComment= -if '$make eq "samsung"' *.jpg    # delete comment field in pictures taken with Samsung camera (ex: Galaxy Note)
jhead -di *.jpg                # delete the IPTC section
jhead -dx *.jpg                # delete the XMP section
jhead -ft *.jpg                # set the file's system time stamp to what is stored in the Exif header
jhead -dsft *.jpg               # set the Exif timestamp to the file's timestamp
exiftool -delete_original! *.jpg *.JPG *.jpeg      # delete the .original backup files that were automatically created
rm *.jpg_original *.JPG_original *.jpeg_original   # remove any additional .original files that weren't removed with the previous command
kdialog --title "Process Complete" --passivepopup "The images have been processed!"

One last question, though, I would like the script to work by just right clicking somewhere in the folder without having to first select all the pictures.
I've included "inode/directory" in the MimeType, but that doesn't seem to be enough.
Anyone know how this could be accomplished?
wolfi323
Registered Member
Posts
1129
Karma
11
OS
thyeum wrote:Thanks for your ideas.
I tried krename, but while the GUI is very powerful, the command line options are very scarce and did not work here.

Well, I have to admit that I never used it from the command line. I just noticed that the options seem to be there...

wolfi323 wrote:One last question, though, I would like the script to work by just right clicking somewhere in the folder without having to first select all the pictures.
I've included "inode/directory" in the MimeType, but that doesn't seem to be enough.
Anyone know how this could be accomplished?

Looking at the krename service files, it seems to be necessary to add "inode/directory" to the "ServiceTypes" line, not the MimeType:
Code: Select all
# Sending all kinds of file to Krename
[Desktop Entry]
Type=Service
Name=Rename with KRename
ServiceTypes=KonqPopupMenu/Plugin,all/allfiles,inode/directory
X-KDE-ExcludeServiceTypes=kdedevice/*
Actions=rename;

[Desktop Action rename]
Exec=krename %U
...
Code: Select all
# Sending all kinds of files recursively from a directory to Krename
[Desktop Entry]
Type=Service
Name=Rename with KRename (with subfolders)
ServiceTypes=KonqPopupMenu/Plugin,inode/directory
X-KDE-ExcludeServiceTypes=kdedevice/*
Actions=rename;

[Desktop Action rename]
Exec=krename -r %U
...
thyeum
Registered Member
Posts
8
Karma
0
OS
Thanks for the tips.
Unfortunately I tried modifying the script as you said, but it still doesn't work.
So unless someone else another idea, I'll probably stick to what I have right now.
wolfi323
Registered Member
Posts
1129
Karma
11
OS
thyeum wrote:Unfortunately I tried modifying the script as you said, but it still doesn't work.

And what's the problem exactly?

I noticed that you wrote "I would like the script to work by just right clicking somewhere in the folder without having to first select all the pictures.", but I think this will only work if you actually right-click *on* a folder, not somewhere *in* a folder.
thyeum
Registered Member
Posts
8
Karma
0
OS
You are right, I was clicking *inside* the folder, but the files were not modified. The script was being run, because I was asked for the prefix (first command) and I got the popup message from the last command, but none of the files were renamed.
I just tried again clicking *on* the folder from the parent directory this time as you suggest, but I get the same results: ask for prefix, show final popup, but no renaming.
I also tried various combinations using:
    %U, %F, nothing
    ServiceTypes=KonqPopupMenu/Plugin,inode/directory or ServiceTypes=KonqPopupMenu/Plugin,all/allfiles,inode/directory
    including or not X-KDE-ExcludeServiceTypes=kdedevice/* (not sure what that's for?)
    including or not MimeType=image/jpeg; or MimeType=image/jpeg;inode/directory;
Having to click *on* the folder wouldn't necessarily be a time-saver for me anyway as I'm usually already inside the folder and Ctrl+A would be as fast as going up one folder. So unless I can get it to work by clicking anywhere *inside* the folder (like it is in Unity) then I'll stick to selecting the pictures and right clicking on one.
Thanks for your help!
airdrik
Registered Member
Posts
1854
Karma
5
OS
Looks like your script is set up to expect things in the current working directory.
You might try adding a line to cd into the specified directory at the top of your script.


airdrik, proud to be a member of KDE forums since 2008-Dec.
thyeum
Registered Member
Posts
8
Karma
0
OS
You're right, but like I said, having to go up one directory to be able to click *on* the folder is not a time saver for me.
So I'll stick to selecting the pictures and clicking on them, if I can't have this working by clicking somewhere *in* the folder.
Thanks for your help though!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], rockscient, Yahoo [Bot]