{"id":71231,"date":"2023-02-15T09:02:49","date_gmt":"2023-02-15T09:02:49","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=71231"},"modified":"2023-02-15T09:02:49","modified_gmt":"2023-02-15T09:02:49","slug":"how-i-use-ansible-to-add-a-feature-to-my-linux-kde-desktop","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=71231","title":{"rendered":"How I use Ansible to add a feature to my Linux KDE desktop"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">How I use Ansible to add a feature to my Linux KDE desktop<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/seth\" class=\"username\">sethkenlon<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Wed, 02\/15\/2023 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>I run the KDE Plasma Desktop on my computer because it&#8217;s a <a href=\"https:\/\/opensource.com\/article\/19\/12\/linux-kde-plasma\">flexible environment<\/a> with lots of options for customization. Having choices in your desktop is about more than just having lots of menus and buttons to activate or deactivate. The thing I love most about KDE Plasma Desktop is the ability to add my own features to it. One reason this is possible is KServices, a simple but powerful plugin framework for handling desktop services.<\/p>\n<h2>Add functions to the right-click menu<\/h2>\n<p>In the KDE Plasma Desktop, there&#8217;s usually a contextual menu available when you right-click on something, such as a directory or a file. You can add items to the right-click menu by creating your own KService, and you don&#8217;t need anything more than a rudimentary understanding of Bash to make it work.<\/p>\n<p>First, create a new directory for your service menu:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">$ mkdir -p ~\/.local\/share\/kio\/servicemenus<\/code><\/pre>\n<p>Add a <code>.desktop<\/code> file:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">$ touch ~\/.local\/share\/kio\/servicemenus\/hello.desktop<\/code><\/pre>\n<p>Open the <code>hello.desktop<\/code> file in a text editor. A <code>.desktop<\/code> file is a small configuration file used by the menu system of the Linux desktop. Here&#8217;s a simple KServices file that generates a <code>hello.txt<\/code> file in the directory you select:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-text\" data-lang=\"text\">[Desktop Entry]\nType=Service\nMimeType=inode\/directory;\nActions=Hello\n\n[Desktop Action Hello]\nName=Say hello\nIcon=\/usr\/share\/icons\/breeze-dark\/actions\/symbolic\/file-library-symbolic.svg\nExec=touch %f\/hello.txt<\/code><\/pre>\n<ul>\n<li>\n<p>The first configuration block tells your system that this <code>.desktop<\/code> file is a service rather than, for instance, an application.<\/p>\n<\/li>\n<li>\n<p>The <code>MimeType<\/code> tells the Plasma Desktop to only show this action as an option when you right-click on a folder, not a file.<\/p>\n<\/li>\n<li>\n<p>The <code>Actions<\/code> line identifies what action is taken when this service is activated. The name <code>Hello<\/code> is arbitrary, and refers to the next configuration block, which is a <code>Desktop Action<\/code> configuration with the name <code>Hello<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>Here&#8217;s what the next configuration block means:<\/p>\n<ul>\n<li>\n<p>The <code>Desktop Action<\/code> definition named <code>Hello<\/code>.<\/p>\n<\/li>\n<li>\n<p>The values for <code>Name<\/code> and <code>Icon<\/code> appear in the right-click menu.<\/p>\n<\/li>\n<li>\n<p>The <code>Exec<\/code> line is the command you want to run when your service is selected. As a simple demonstration, this <code>.desktop<\/code> file just creates an empty file called <code>hello.txt<\/code> in the location that you right-clicked on (represented by the special variable <code>%f<\/code>).<\/p>\n<\/li>\n<\/ul>\n<p>Save the <code>.desktop<\/code> file, and then make it executable:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">$ chmod +x ~\/.local\/share\/kio\/servicemenus\/hello.desktop<\/code><\/pre>\n<p>Start a new instance of the <a href=\"https:\/\/opensource.com\/article\/22\/12\/linux-file-manager-dolphin\" target=\"_blank\" rel=\"noopener\">Dolphin file manager<\/a> and create an empty folder. Then right-click on the folder and navigate to <strong>Actions<\/strong>. In the <strong>Actions<\/strong> menu, there&#8217;s a new service available, and it&#8217;s called <strong>Say hello<\/strong> because that&#8217;s what your <code>.desktop<\/code> file has set in the <code>Name<\/code> field.<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/02\/kde-plasma-desktop-kservices.png\" width=\"731\" height=\"436\" alt=\"\u200bImage showing that KServices appears in the right-click menu of Dolphin. \"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>(Seth Kenlon, CC BY-SA 4.0)<\/p>\n<\/div>\n<\/article>\n<p>Look in the folder after running the service to see the empty <code>hello.txt<\/code> file that&#8217;s been created.<\/p>\n<h2>Mimetypes<\/h2>\n<p>This sample KService only works on directories because the <code>.desktop<\/code> file defining the service specifies the <code>Mimetype: inode\/directory<\/code>. That&#8217;s what tells Dolphin not to display the service when you right-click on a file.<\/p>\n<p>Using mimetypes, you can create highly specific services based on what kind of file system object you select when you right-click. The <code>perl-file-mimeinfo<\/code> package provides a <code>mimetype<\/code> command, which you can use to get the mimetype of any file. Install it with your distribution&#8217;s package manager, and then try it out:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">$ mimetype example.txt\nexample.txt: text\/plain\n$ mimetype Photos\/example.webp\nPhotos\/example.webp: image\/webp<\/code><\/pre>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More Linux resources<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/linux-commands-cheat-sheet\/?intcmp=70160000000h1jYAAQ\">Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/advanced-linux-commands\/?intcmp=70160000000h1jYAAQ\">Advanced Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/rh024-red-hat-linux-technical-overview?intcmp=70160000000h1jYAAQ\">Free online course: RHEL technical overview<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-networking?intcmp=70160000000h1jYAAQ\">Linux networking cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-selinux?intcmp=70160000000h1jYAAQ\">SELinux cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/linux-common-commands-cheat-sheet?intcmp=70160000000h1jYAAQ\">Linux common commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/resources\/what-are-linux-containers?intcmp=70160000000h1jYAAQ\">What are Linux containers?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux?intcmp=70160000000h1jYAAQ\">Our latest Linux articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Executables<\/h2>\n<p>I demonstrated a simple &#8220;hello world&#8221; example in this article, but KServices can be as complex as you need them to be. The <code>Exec<\/code> line of your KService <code>.desktop<\/code> file can launch any application or script, and the <code>%f<\/code> variable ensures that the target or destination of whatever gets launched is what you&#8217;ve right-clicked on.<\/p>\n<p>For my own workflow, I used to use <a href=\"https:\/\/opensource.com\/article\/17\/7\/managing-creative-assets-planter\">Planter<\/a> to quickly construct a project environment. Lately, though, I&#8217;ve switched to Ansible and this KService:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">[Desktop Entry]\nType=Service\nMimeType=inode\/directory;\nActions=planter\n\n[Desktop Action planter]\nName=Create project directory\nIcon=\/usr\/share\/icons\/breeze-dark\/actions\/symbolic\/folder-new-symbolic.svg\nExec=ansible-playbook \/home\/seth\/Ansible\/playbooks\/standard_dirs.yaml -e dir=%f<\/code><\/pre>\n<p>Here&#8217;s my Ansible playbook:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-yaml\" data-lang=\"yaml\">---\n- hosts: localhost\n  tasks:\n    - name: Create directories\n      ansible.builtin.file:\n        path: \"{{ item }}\"\n        state: directory\n      with_items:\n        - '{{ dir }}\/video'\n        - '{{ dir }}\/edit'\n        - '{{ dir }}\/audio'\n        - '{{ dir }}\/title'\n        - '{{ dir }}\/render'\n        - '{{ dir }}\/graphic'\n        - '{{ dir }}\/photo'<\/code><\/pre>\n<p>When I right-click on a directory and select <strong>Create project directory<\/strong>, the subdirectories I need for media projects are added to that directory. It&#8217;s a simple feature for a desktop, and a little unique to a specific workflow, but it&#8217;s the feature I want. And thanks to KServices, it&#8217;s a feature I have. Try out KServices in the KDE Plasma Desktop for yourself, and add the feature you want.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Follow this tutorial to see how I use KService and Ansible on my Linux KDE desktop.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/02\/code_computer_laptop_hack_work-1.png\" width=\"1041\" height=\"584\" alt=\"Coding on a computer\" title=\"Coding on a computer\"><\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux\" hreflang=\"en\">Linux<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/ansible\" hreflang=\"en\">Ansible<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/02\/cc-by-sa--22.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How I use Ansible to add a feature to my Linux KDE desktop sethkenlon Wed, 02\/15\/2023 &#8211; 03:00 I run the KDE Plasma Desktop on my computer because it&#8217;s a flexible environment with lots of options for customization. Having choices in your desktop is about more than just having lots of menus and buttons to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":71232,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-71231","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/71231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71231"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/71231\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/71232"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}