Scripting

Advanced Renamer forum
#1 : 08/03-14 02:53
peter
peter
Posts: 3
Hi,

I just came across this software and it's great (really capable) but I was wondering if somebody can help me with one issue. I often rename files that contain polish characters ( ąźĘŹĆŚół, etc. ). Is there a way to apply any of the java scripts to do this? I found couple of such scripts but I can't to make them work.
Here's exampe:

'use strict';

var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
PluginUpdatedAt = require('./plugin_updated_at');

module.exports = function (app) {

var Article = new Schema({
title: { type: String, trim: true },
title_urlized: { type: String, validate: /[a-zA-Z0-9\-]/, required: true, unique: true },
created_at: { type: Date, default: Date.now, required: true, index: true },
external_url: { type: String, validate: [/^https?:\/\//i, 'Incorrect URL'], required: true, trim: true },
summary: String
});
Article.plugin(PluginUpdatedAt);

Article.path('title').set(function (v) {
var reps = {
Ä™: 'e',
Ä…: 'a',
Å›: 's',
Å‚: 'l',
ż: 'z',
ź: 'z',
ć: 'c',
Å„: 'n',
Ę: 'E',
Ä„: 'A',
Åš: 'S',
Ł: 'L',
Å»: 'Z',
Ź: 'Z',
Ć: 'C',
Ń: 'N',
' ': '-'
};

if (!this.title_urlized) {
this.title_urlized = v.replace(/[^a-zA-Z0-9\-]/g, function (c) {
return reps[c] || '';
}).toLowerCase();
}

return v;
});

return Article;

};


or yet another one:



String.prototype.escapeDiacritics = function()
{
return this.replace(/Ä…/g, 'a').replace(/Ä„/g, 'A')
.replace(/ć/g, 'c').replace(/Ć/g, 'C')
.replace(/ę/g, 'e').replace(/Ę/g, 'E')
.replace(/ł/g, 'l').replace(/Ł/g, 'L')
.replace(/ń/g, 'n').replace(/Ń/g, 'N')
.replace(/ó/g, 'o').replace(/Ó/g, 'O')
.replace(/Å›/g, 's').replace(/Åš/g, 'S')
.replace(/ż/g, 'z').replace(/Ż/g, 'Z')
.replace(/ź/g, 'z').replace(/Ź/g, 'Z');
}
// przykładowe wykorzystanie:
var test = new String("ąźĘŹĆŚół");
alert(test.escapeDiacritics());

or this script:

function clearDiactricts(input:String):String {
    var pattern:RegExp = /ó/g;
    input = input.replace(pattern,"o");
    pattern = /Ó/g;
    input = input.replace(pattern,"O");
    pattern = /ł/g;
    input = input.replace(pattern,"l");
    pattern = /Ł/g;
    input = input.replace(pattern,"L");
    pattern = /ń/g;
    input = input.replace(pattern,"n");
    pattern = /Ń/g;
    input = input.replace(pattern,"N");
    pattern = /ż/g;
    input = input.replace(pattern,"z");
    pattern = /Ż/g;
    input = input.replace(pattern,"Z");
    pattern = /ź/g;
    input = input.replace(pattern,"z");
    pattern = /Ź/g;
    input = input.replace(pattern,"Z");
    pattern = /Ć/g;
    input = input.replace(pattern,"C");
    pattern = /ć/g;
    input = input.replace(pattern,"c");
    pattern = /ę/g;
    input = input.replace(pattern,"e");
    pattern = /Ę/g;
    input = input.replace(pattern,"E");
    pattern = /Ś/g;
    input = input.replace(pattern,"S");
    pattern = /ś/g;
    input = input.replace(pattern,"s");
    pattern = /ą/g;
    input = input.replace(pattern,"a");
    pattern = /Ą/g;
    input = input.replace(pattern,"A");
    return input;      
}


08/03-14 02:53
#2 : 11/03-14 13:43
Stefan
Stefan
Posts: 274
Reply to #1:

Hi Peter,

the script are fine, but you have to simplify them and use only the needed parts, like:

Paste this into Advanced Renamer Script Method
(http://www.advancedrenamer.com/user_guide/metho d_script)


[code]
newname = item.name;
newname = newname.replace(/a/g,'X');
newname = newname.replace(/c/g,'B');
newname = newname.replace(/d/g,'Z');
// and so on...

return newname;
[/code]


Explanation:
newname >>>is just a variable name, you could use just "nn" or "Peter" too.
item.name >> is ARens Variable to provide the current processed file name (start typing 'item.' to see more)
.replace(/ findthis /flag, "replacement"); >> is a common JavaScript method.
Flags here can be "g" for global, means replace all instead of first occurrence only,
and "i" for ignore case (a==A), (and "m" for multiline makes no sense here)
return >>> is a ARen directive and a JavaScript keyword to return the calculated result of this function.

.


11/03-14 13:43 - edited 11/03-14 13:45
#3 : 13/03-14 00:26
peter
peter
Posts: 3
Hi Stefan,

Thank you for the reply, but I still can't make it work. My knowledge about scripting is very limited.
Can you post full code example "ready to go"?
For example to remove the Ä and Å and � charcter from the string or any other "weird" characters.

Thank you in advance.


13/03-14 00:26
#4 : 13/03-14 08:08
Stefan
Stefan
Posts: 274
Reply to #3:
>>>For example to remove the � and � and � charcter from the string or any other "weird" characters.


That is a fully working script

Just replace:
newname = newname.replace(/a/g,'X');

With your chars like:
newname = newname.replace(/Ã?/g,'Ã?');



If this doesn't work, it maybe because of not support unicode characters.
Since I don't use unicode and I don't see your posted chars (only "garbage"
due to wrong webpage encoding) I am afraid I am not of great help here.

Try to find a working standalone script which works for your issue
and we will try to move that to an AdvanceRenamer Script for you.


If I find something fitting, I will repost too.


.


13/03-14 08:08
#5 : 13/03-14 08:12
Stefan
Stefan
Posts: 274
Reply to #4:

Arrggh.

@Kim

I have modified JS code and pressed [Apply Script] and it doesn't did anything.

At many tries I realised that the Script Method was not enabled. :D

So there is room for improvement. Maybe show a message in
that case like "No effect, Method is disabled", or just disable
that button too if the whole Method is disabled.


13/03-14 08:12