Question

I have a two part question here. I'm trying to make an automator service to quickly rename files. My job uses a file name syntax of 1234-01a-FileName.psd where 1234 is the job number, 01 the file number, and the a is a version letter, as in, version two will be 01b, etc. Getting the current letter with RegEx and incrementing it is easy, the issue I'm having is how to actually rename the file with JXA. Nothing I can find on google is actually answering this (I assume) simple question. I've tried a number of permutations of the following. Is there something basic I'm doing wrong here?

theFile = Path("~/Desktop/1234-12a-test.png");
theFile.name = "thisDoesntWork.png";

I could alternately have both a Run AppleScript and a Run Javascript in the automator workflow, and try to pass the name back and forth, but I'd rather keep it simple if possible.

Was it helpful?

Solution

The Path object is a very basic structure and doesn't provide any actual filesystem functionality. Instead, use System Events:

theFile = Application("System Events").aliases.byName("~/Desktop/1234-12a-test.png");
theFile.name = "thisDoesWork.png"
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top