Reputation: 12351
Can anyone tell me how i could go about appending filenames in a specific folder with the string "@2x". I would prefer not to go to the trouble of renaming every single file by hand. Thanks.
Upvotes: 0
Views: 1144
Reputation: 7191
You can do this with Automator without any code.
Or, as you already know "Xcode" and Objective-C, here's an example that adds @2X
before the name extension.
BOOL result;
NSString *fullPath, *filename, *newName;
NSString *dir = @"/Users/jack/Desktop/Untitled 1";
NSFileManager *fm = [NSFileManager defaultManager];
NSEnumerator *enumerator = [[fm contentsOfDirectoryAtPath:dir error:nil] objectEnumerator];
while(filename=[enumerator nextObject]){
if ([filename hasPrefix:@"."]) { continue;}//skip files that begin with a dot
fullPath = [dir stringByAppendingPathComponent:filename];
if (!([fm fileExistsAtPath:fullPath isDirectory:&result]&&result)) { // skip directory
if ([[filename pathExtension] isEqualToString:@""]) {
newName = [fullPath stringByAppendingString:@"@2X"];// no extension
} else {
newName = [[fullPath stringByDeletingPathExtension] stringByAppendingFormat:@"@2X.%@",[filename pathExtension]];
}
result = [fm moveItemAtPath:fullPath toPath:newName error:nil];
}
}
Upvotes: 1
Reputation: 27073
Launch AppleScript Editor and paste the following script:
set appendable to "@2x"
set theFolder to choose folder
tell application "Finder"
set theFiles to (files of entire contents of theFolder) as alias list
repeat with theFile in theFiles
set FileExtension to theFile's name extension as string
set FileName to theFile's name as string
set FileBaseName to text 1 thru ((offset of "." in FileName) - 1) of FileName
set theFile's name to FileBaseName & appendable & "." & FileExtension
end repeat
end tell
The script appends "@2x" to all files within the selected folder.
Simply hit the "Run" button and select any folder to execute the script.
Upvotes: 3