{"id":757,"date":"2020-03-19T15:43:00","date_gmt":"2020-03-19T14:43:00","guid":{"rendered":"https:\/\/merseli.com\/?p=757"},"modified":"2020-06-03T11:05:20","modified_gmt":"2020-06-03T09:05:20","slug":"python-scripts-for-notepad","status":"publish","type":"post","link":"https:\/\/merseli.com\/en\/2020\/03\/19\/python-scripts-for-notepad\/","title":{"rendered":"Python Scripts for Notepad++"},"content":{"rendered":"\n<p>Sometimes working on different platforms happens to move sources from one place to another, usually this simple copy-paste operation does not cause problems, but if you work with <a href=\"https:\/\/www.eclipse.org\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Eclipse (opens in a new tab)\">Eclipse<\/a>, the sources are treated as simple text files so the file encoding becomes important to avoid surprises especially if there are many sources.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1128\" height=\"571\" src=\"https:\/\/i2.wp.com\/merseli.com\/wp-content\/uploads\/2020\/03\/1.png?fit=1024%2C518&amp;ssl=1\" alt=\"\" class=\"wp-image-758\" srcset=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/1.png 1128w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/1-300x152.png 300w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/1-1024x518.png 1024w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/1-768x389.png 768w\" sizes=\"auto, (max-width: 1128px) 100vw, 1128px\" \/><\/a><figcaption>Wrong text encoding<\/figcaption><\/figure>\n\n\n\n<p>Usually in Eclipse the text files are encoded as &#8220;Cp1252&#8221;, therefore if you use the classic Latin characters it is necessary that the files are encoded in &#8220;UTF-8&#8221; and the first option to modify in the IDE is the following.<\/p>\n\n\n\n<p>In <em>Windows &#8211; Preferences &#8211; General &#8211; Workspace &#8211; Text file encoding<\/em>, select <em>Other: UTF-8<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1680\" height=\"1011\" src=\"https:\/\/i1.wp.com\/merseli.com\/wp-content\/uploads\/2020\/03\/2.png?fit=1024%2C616&amp;ssl=1\" alt=\"\" class=\"wp-image-759\" srcset=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/2.png 1680w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/2-300x181.png 300w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/2-1024x616.png 1024w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/2-768x462.png 768w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/2-1536x924.png 1536w\" sizes=\"auto, (max-width: 1680px) 100vw, 1680px\" \/><\/a><\/figure>\n\n\n\n<p>At this point proceed importing the sources into the project.<\/p>\n\n\n\n<p>If the sources have already been imported, it is necessary to convert them to the correct encoding.<\/p>\n\n\n\n<p>Here you can use a very useful tool to run macros and scripts written in Python on <a rel=\"noreferrer noopener\" aria-label=\"Notepad++ (opens in a new tab)\" href=\"https:\/\/notepad-plus-plus.org\/\" target=\"_blank\">Notepad++<\/a>, the plugin is called Python Script and it can be installed from the Notepad++ Plugin Manager.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"689\" src=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/3.png\" alt=\"\" class=\"wp-image-760\" srcset=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/3.png 941w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/3-300x220.png 300w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/3-768x562.png 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><\/a><\/figure>\n\n\n\n<p>To create a new script from the <em>Plugin &#8211; Python Script &#8211; New Script menu<\/em>, give a name to the script you want to create.<\/p>\n\n\n\n<p>In this case, the script to convert the encoding of files into UTF-8 with BOM is as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; auto-links: false; gutter: false; title: ; notranslate\" title=\"\">\nimport os;\nimport sys;\nfilePathSrc=&quot;D:\\\\eclipse\\\\eclipse-workspace\\\\LibroJava11\\\\src\\\\LibroJava*11&quot;\nfor root, dirs, files in os.walk(filePathSrc):\n    for fn in files:\n      if fn&#x5B;-4:] != &#039;.jar&#039; and fn&#x5B;-5:] != &#039;.ear&#039; and fn&#x5B;-4:] != &#039;.gif&#039; and fn&#x5B;-4:] != &#039;.jpg&#039; and fn&#x5B;-5:] != &#039;.jpeg&#039; and fn&#x5B;-4:] != &#039;.xls&#039; and fn&#x5B;-4:] != &#039;.GIF&#039; and fn&#x5B;-4:] != &#039;.JPG&#039; and fn&#x5B;-5:] != &#039;.JPEG&#039; and fn&#x5B;-4:] != &#039;.XLS&#039; and fn&#x5B;-4:] != &#039;.PNG&#039; and fn&#x5B;-4:] != &#039;.png&#039; and fn&#x5B;-4:] != &#039;.cab&#039; and fn&#x5B;-4:] != &#039;.CAB&#039; and fn&#x5B;-4:] != &#039;.ico&#039;:\n        notepad.open(root + &quot;\\\\&quot; + fn)\n        console.write(root + &quot;\\\\&quot; + fn + &quot;\\r\\n&quot;)\n        #Does not work --&gt; notepad.runMenuCommand(&quot;Encoding&quot;, &quot;Character sets&quot;, &quot;Chinese&quot;, &quot;GB2312 (Simplified)&quot;)\n        # notepad.menuCommand(MENUCOMMAND.FORMAT_GB2312)\n        # notepad.runMenuCommand(&quot;Encoding&quot;, &quot;Convert to UTF-8-BOM&quot;)\n        notepad.menuCommand(MENUCOMMAND.FORMAT_CONV2_UTF_8)\n        # Reference: https:\/\/github.com\/bruderstein\/PythonScript\/blob\/master\/PythonScript\/src\/NotepadPython.cpp\n        notepad.save()\n        notepad.close()\n<\/pre><\/div>\n\n\n<p>Note that I entered the path containing the sources in the &#8220;<em>filePathSrc<\/em>&#8221; string and with the &#8220;<em>notepad.menuCommand<\/em>&#8221; method I passed the &#8220;<em>MENUCOMMAND.FORMAT_CONV2_UTF_8<\/em>&#8221; command which encodes the file.<\/p>\n\n\n\n<p>Save the script once complete.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"951\" height=\"578\" src=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/4.png\" alt=\"\" class=\"wp-image-761\" srcset=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/4.png 951w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/4-300x182.png 300w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/4-768x467.png 768w\" sizes=\"auto, (max-width: 951px) 100vw, 951px\" \/><\/a><\/figure>\n\n\n\n<p>To launch the script from the <em>Plugins &#8211; Python Scripts &#8211; Script<\/em> menu and select the script created.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"623\" src=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/5.png\" alt=\"\" class=\"wp-image-762\" srcset=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/5.png 730w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/5-300x256.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/a><\/figure>\n\n\n\n<p>At this point, reloading the source into Eclipse it can be verified that the file encoding is now correct.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1263\" height=\"623\" src=\"https:\/\/i0.wp.com\/merseli.com\/wp-content\/uploads\/2020\/03\/6.png?fit=1024%2C505&amp;ssl=1\" alt=\"\" class=\"wp-image-763\" srcset=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/6.png 1263w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/6-300x148.png 300w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/6-1024x505.png 1024w, https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/6-768x379.png 768w\" sizes=\"auto, (max-width: 1263px) 100vw, 1263px\" \/><\/a><\/figure>\n\n\n\n<p>For more information about the Python Scripts plugin and for many other useful scripts visit the developer&#8217;s GitHub page.<\/p>\n\n\n\n<div class=\"wp-block-button is-style-outline is-style-outline--1\"><a class=\"wp-block-button__link has-background has-very-light-gray-background-color\" href=\"https:\/\/notepad-plus-plus.org\/\">Notepad++ Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-button is-style-outline is-style-outline--2\"><a class=\"wp-block-button__link has-background has-very-light-gray-background-color\" href=\"https:\/\/github.com\/bruderstein\/PythonScript\/tree\/master\/PythonScript\">Python Script GitHub<\/a><\/div>\n\n\n\n<p> <\/p>\n\n\n\n<div class=\"wp-block-button is-style-outline is-style-outline--3\"><a class=\"wp-block-button__link has-background has-very-light-gray-background-color\" href=\"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/ConvertToUFT-8-BOM.txt\">ConvertToUFT-8-BOM.py<\/a><\/div>\n\n\n\n<p>Rename <em>ConvertToUFT-8-BOM.txt<\/em> to <em>ConvertToUFT-8-BOM.py <\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes working on different platforms happens to move sources from one place to another, usually this simple copy-paste operation does not cause problems, but if you work with Eclipse, the sources are treated as simple text files so the file encoding becomes important to avoid surprises especially if there are many sources. Wrong text encoding [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":770,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_vp_format_video_url":"","_vp_image_focal_point":[],"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[35],"tags":[37,38,36],"class_list":["post-757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tip-tricks","tag-notepad","tag-plugin","tag-python-script"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/merseli.com\/wp-content\/uploads\/2020\/03\/PythonScriptNotepadPP-1.png","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/posts\/757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/comments?post=757"}],"version-history":[{"count":5,"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/posts\/757\/revisions"}],"predecessor-version":[{"id":1222,"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/posts\/757\/revisions\/1222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/media\/770"}],"wp:attachment":[{"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/media?parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/categories?post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merseli.com\/en\/wp-json\/wp\/v2\/tags?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}