Thank you, Martin, very much for the explanation. Unfortunately, the macro does not work with rtf files containing binary data. I tested it with an unzipped zrtf file (Release Notes.zrtf), for which
Cast to String seems to return nothing. I'd like to have something like
Cast to String $data, bytes (feature request ;-)
With ordinary rtf files, the macro does not have a problem, though. Images are stored in them as ASCII strings.
But I have not checked all kinds of objects and this may change in a future version of Nisus Writer Pro, for example, when it supports audio or movie (?). Thus, I modified the macro so that it uses Perl to change the values of the rtf directives.
Also, now you can define
$folderNameSuffix and
$fileNameSuffix. If you set
$folderNameSuffix to something, converted files will be saved in a new folder (folder name +
$folderNameSuffix) under the same parent directory. In that case, you can set
$fileNameSuffix to nothing (
$fileNameSuffix = '') so that the converted files have the same file names.
With
$folderNameSuffix set to nothing (
$folderNameSuffix = '') and
$fileNameSuffix set to something (e.g.
$fileNameSuffix = ' (US Letter)'), the macro works almost in the same way as the first version.
Code: Select all
### Change Paper Size ###
# From rtf files in a folder, this macro generates new files with page size set to $width and $height.
# The macro does not change margins. So resulting files may not look good.
# Only files in RTF format (rtf, nwm and non-binary doc) are processed.
# Converted files (file name + $fileNameSuffix) will be saved in a new folder (folder name + $folderNameSuffix) under the same parent directory. However, if you set $folderNameSuffix to nothing ($folderNameSuffix = ''), converted files will be saved in the source folder.
# If you set $fileNameSuffix to nothing ($fileNameSuffix = ''), the same file names will be used. In that case, you cannot set $folderNameSuffix to nothing.
# Files for which a converted file already exists will be skipped.
# If "Keep prior copy of each files using the file name suffixv is enabled in "Preferences - Saving", backup files will be ignored.
Require Pro Version 1.3
$folderNameSuffix = ' (US Letter)' # settings for US Letter
$fileNameSuffix = ''
$width = 12240
$height = 15840
#$folderNameSuffix = ' (A4)' # settings for A4
#$fileNameSuffix = '_A4'
#$width = 11900
#$height = 16840
#$folderNameSuffix = ' (JB5)' # settings for JB5
#$fileNameSuffix = '_JB5'
#$width = 10320
#$height = 14580
if $folderNameSuffix == undefined
if $fileNameSuffix == undefined
exit 'You have to define $fileNameSuffix and $folderNameSuffix, one of them at least.'
end
end
$defaultFolderPath = '~/Documents'
$openButtonCaption = 'Choose a Folder'
$folderPath = Choose Folder $defaultFolderPath, $openButtonCaption
if $folderPath == undefined
exit # cancelled by the user
end
$fileNames = File.namesInFolderAtPath $folderPath
$keepLastCopy = Read User Default 'com.nisus.backup.keepLastCopy'
if $keepLastCopy == true
$lastCopySuffix = Read User Default 'com.nisus.backup.lastCopySuffix'
$lastCopySuffix = '\Q' & $lastCopySuffix
$lastCopySuffix &= '.\E[^.]+$'
end
$newPaths = Hash.new
$newFolder = $folderPath & $folderNameSuffix
foreach $fileName in $fileNames
$notLastCopy = true
if $keepLastCopy == true
if $fileName.find $lastCopySuffix, 'E-i'
$notLastCopy = false
end
end
if $notLastCopy == true
$filePath = $folderPath.filePathByAppendingComponent $fileName
$filePath = File.resolveLinkAtPath $filePath
$newPath = $newFolder.filePathByAppendingComponent $fileName
$newPath.findAndReplace '(?=\.[^.]+$)', $fileNameSuffix, 'E'
$newPaths{$filePath} = $newPath
end
end
$errors = ''
Set Exported Perl Variables 'newPaths', 'newFolder', 'width', 'height', 'errors'
Set Include Perl UTF Preamble false
begin Perl
foreach my $path (keys (%newPaths)) {
my $newPath = $newPaths{$path};
if ( -e $newPath ) {
$errors .= "'$newPath' already exists.\n";
} else {
my $fileType = `/usr/bin/file -b --mime "$path"`;
$fileType =~ s/[\n\r]+$//;
if ( $fileType eq 'text/rtf' ) {
if ( ! -f $newFolder ) {
`mkdir -p "$newFolder"`;
};
open (IN, "<:bytes", $path);
open (OUT, ">:bytes", $newPath);
local undef $/;
my $data = <IN>;
$data =~ s/(?<=\x5Cpaperw|\x5Cpgwsxn)[0-9]+/$width/g;
$data =~ s/(?<=\x5Cpaperh|\x5Cpghsxn)[0-9]+/$height/g;
print OUT $data;
close IN;
close OUT;
} else {
$errors .= "'$path' is not an RTF file.\n";
}
}
};
end
File.revealPathInFinder $newFolder
if $errors
exit $errors
end
### end of macro ###