• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I use the containing folder's name to rename files on export

Explorer ,
Oct 14, 2013 Oct 14, 2013

Copy link to clipboard

Copied

I'm trying to write a script that takes the folder name that the active document resides in and uses it as part of a new name.

So, let's say I have a file called "strawberries.jpg" that is 1000x1000px and resides in a folder called "good-fruit". I want the script to export the final file as a TIFF like this:

good-fruit/good-fruit-strawberries-1000x1000.tiff

Thus far, I've managed to use alerts to figure out how to grab document name, beginning size, current folder path:

var doc = app.activeDocument;

var dirName = doc.path;

var cropWidth = doc.width.as('px');

var cropHeight = doc.height.as('px');

alert("This document is " + doc.name + " and is " + cropWidth + "x" + cropHeight + " and resides in: " + dirName );

Which results in an alert that says: This document is strawberries.jpg and is 1000x1000 and resides in: ~/Desktop/Folder/good-fruit/

But I cannot figure out how to cut off the beginning of doc.path (i.e. ~/Desktop/Folder/) so I can just add "good-fruit" to the final name. I also can't figure out how to insert "good-fruit" before the extension of the image.

Thanks for any help!

Joe

TOPICS
Actions and scripting

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Contributor ,
Oct 14, 2013 Oct 14, 2013

Copy link to clipboard

Copied

Bonjour

You can see HERE

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 14, 2013 Oct 14, 2013

Copy link to clipboard

Copied

To get the name of the folder that contains the document's file use

decodeURI(app.activeDocument.fullName.parent.name)

Add that to the RegExp example above for removing the extension and you get

saveName = decodeURI(app.activeDocument.fullName.parent.name)+'-'+ app.activeDocument.name.replace(/\.[^\.]+$/, '')+'.tif';

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 14, 2013 Oct 14, 2013

Copy link to clipboard

Copied

Thanks dominique and Michael, you rock!

Joe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 14, 2013 Oct 14, 2013

Copy link to clipboard

Copied

If either of you have time, could you break down what the following bit of your code means:

/\.[^\.]+$/

I've seen it before and assume it is some kind of search pattern? I'm curious exactly how it works and what each part means.

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 15, 2013 Oct 15, 2013

Copy link to clipboard

Copied

LATEST

It is regular expression and yes it is use for pattern matching. The forward slashes are used to start and end the expression when it is create this way( literal ). Another way to create one would be to make an explicit declaration var re = new RegExp('\.[^\.]+$').

Some chars in regular expressions have special meaning. The dot char normally means match any char. The blackslash is used to escape that special meaning so \. means match the dot char. The [ and ] are used for char sets. Those are normally used when you only want to match specific chars [aeiou] would match any English vowel. The ^ used inside the brackets means 'not' so [^\.] means match any char that is not a dot( or any char except an dot ). If used outside of brackets the ^ char means at the start. The + means match the preceeding one or more times. In this case match any non dot char one or more times. The $ means at the end of the string.

So putting that althougher you have 'match a . followed by one or more chars at the end'.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines