file management

a number of file management extensions featured in wslib


features: 

- moving, copying, renaming and deleting files using unix cmds

- compressing files with tar, gz, zip and flac (if available)

- opening files with other apps and show in Finder

- extensive file name processing and testing


general testing

extensions on String


isFile, isFolder

same as Pathname:isFile / Pathname:isFolder

isBundle

test if it is a bundle

"SuperCollider.app".isBundle; // true

isSoundFile

test if path is a valid soundfile which can be read by SC

"sounds/a11wlk01.wav".isSoundFile; // true

pathExists ( showType )

check if path exists, and if so, return its type as a Symbol (if showType is true)

Doesn't check if it's a soundfile though

"sounds/a11wlk01.wav".pathExists; // \file

"~/scwork".pathExists; // \folder (or false if it doesn't exist)



unix command wrappers

extensions on String


basic file management


makeDir ( silent )

creates a dir if it is not there yet. 

Posts a message if a dir is created and silent == false (default)

Careful: also makes parent dirs if their not there

"~/Desktop/testDir/subDir".makeDir;

renameTo ( newName )

renames a file or folder

Carefull: overwrites existing files or folders by that name without notice

"~/Desktop/testDir/subDir".renameTo( "renamed" );

copyTo ( newDir, newName, overwrite, createIfNotThere, silent )

copies a file or folder to a new location. If overwrite == false (default) it doesn't overwrite any existing files. 

if  createIfNotThere is true ( default ) the newDir is created using makeDir. 

Retrns true at succes

"~/Desktop/testDir".copyTo( "~/Desktop/testCopy" );

moveTo ( newDir, newName, createIfNotThere, silent )

moves a file or folder to a new location. Fails and returns false if the file / folder already exists

"~/Desktop/testCopy/testDir/renamed".moveTo( "~/Desktop/testCopy" ); 

removeFile ( toTrash, ask, silent )

removes a file or folder.

By default toTrash == true, which moves the file to the trash.

If toTrash == false, the file is permanently deleted. 

If ask == true ( default ) && toTrash == false a dialog comes up before anything gets deleted

"~/Desktop/testCopy/testDir".removeFile( false );

"~/Desktop/testCopy/testDir".removeFile; // check the trashcan

copyRename ( newDir, newName, createIfNotThere, silent )

moveRename ( newDir, newName, createIfNotThere, silent )

copies or moves a file or folder to a new location, but automatically renames the file if it already exists

"~/Desktop/testCopy/renamed".copyRename( "~/Desktop/testCopy" ); // copy to same folder

copyReplace ( newDir, newName, createIfNotThere, toTrash, ask, silent )

moveReplace ( newDir, newName, createIfNotThere, toTrash, ask, silent )

copies or moves a file or folder to a new location, but automatically moves any file that was already 

there to the trash or deletes it, using removeFile.

"~/Desktop/testDir/renamed".copyReplace( "~/Desktop/testCopy", toTrash: false );

compression


zip ( newPath, includeInvisible, tmp )

unzip ( to )

creates and unpacks zip files

if newPath  and to  are not specified, they will be automatically created

(

// make a zip file from the plugins folder, after copying it to the desktop

"plugins".copyTo( "~/Desktop/", "plugins copy" );

"~/Desktop/plugins copy".zip;

"~/Desktop/plugins copy".removeFile( false, false ); // remove original

)

"plugins".zip( "~/Desktop/plugins archive" ); // or shortly

"~/Desktop/plugins archive.zip".unzip; // and there they are again

tar ( newPath )

untar

creates and unpacks tar archives

(

// make a tar file from the plugins folder, after copying it to the desktop

"plugins".copyTo( "~/Desktop/", "plugins copy" );

"~/Desktop/plugins copy".tar;

"~/Desktop/plugins copy".removeFile( false, false ); // remove original

)

"plugins".tar( "~/Desktop/plugins archive" ); // or shortly

"~/Desktop/plugins archive.tar".untar; // and there they are again

gz ( speed )

ungz

creates and unpacks gz files. Originals are deleted automatically. 

gz can only compress files, not folders. For folders use tar first.

speed ranges from 1 to 9 (default 6), resembling fast to best

"~/Desktop/plugins archive.tar".gz;

"~/Desktop/plugins archive.tar.gz".ungz;

targz ( newPath, speed ), tgz ( newPath, speed )

untgz ( deleteTar )

creates and unpacks tar.gz and tgz files. 

if deleteTar == true the tar file is automatically deleted by moving to trash. By default it is left where it is

"plugins".targz( "~/Desktop/plugins archive" );

"~/Desktop/plugins archive.tar.gz".untgz( true );

other operations


openWith ( appName )

openWithID ( id )

opens a file with another application, using an appName or id (e.g. "com.apple.safari" )

Document.current.path.openWith( "Safari" );

openInFinder, showInFinder

opens or shows a file or folder with the finder

Document.current.path.dirname.openInFinder;

revealInFinder

opens and selects a file or folder with the finder

Document.current.path.revealInFinder;



file name processing


extension

returns the extension of a path or filename. If no valid extension is found it returns nil.

The String doesn't need to be a path to an existing file.


"sounds/a11wlk01.wav".extension; // "wav"

"SuperCollider.app/".extension; // "app"

"someFile".extension; // nil

"My brilliant app v1.0".extension; // nil ('.0' is not a valid extension)

"My brilliant app v1.0.rtf".extension; // "rtf"

"My brilliant app v1.0.r tf".extension; // nil (extension contains space)


hasExtension

"sounds/a11wlk01.wav".hasExtension; // true

isValidExtension

"wav".isValidExtension; // true

"0".isValidExtension; // false

"".isValidExtension; // false

"blaht".isValidExtension; // true (no size limit)

"a bc".isValidExtension; // no spaces allowed

removeExtension, replaceExtension ( newExt )

"sounds/a11wlk01.wav".removeExtension; // "sounds/a11wlk01"

"sounds/a11wlk01.wav".replaceExtension( "aif" ); // "sounds/a11wlk01.aif"

capsToSpaces ( space, toLower )

spacesToCaps ( space )

converts "wordAndAnotherWord" to "word and another word" and vice versa


"aWordAndAnotherWord".capsToSpaces( "_" ); // -> "a_word_and_another_word"

"a word and another word".spacesToCaps; // -> "aWordAndAnotherWord"


firstToUpper

firstToLower

makes the first char of a string an upper or lower case


"hi".firstToUpper; // -> "Hi"


splitNumbers ( convert )

breaks a string up into an array with separate items for the numbers

If convert == true (default) the numbers are interpreted right away


"a50b10.3c-11".splitNumbers; // -> [ "a", 50, "b", 10.3, "c", -11 ]


extractNumbers

removeNumbers

doToNumbers( func )

these methods all use splitNumbers

"a50b10.3c-11".extractNumbers; // -> [ 50, 10.3, -11 ]

"a50b10.3c-11".removeNumbers; // ->"abc"

"a50b10.3c-11".doToNumbers( _ + 1 ); // -> a51b11.3c-10

numbersToAlpha ( offset, toUpper, prepend, append )

converts all numbers in a string to alpha chars. 

Works only with whole numbers from 0 to 25. Higher or lower numbers stay unchanged

"file12-4".numbersToAlpha; // -> "fileM-4"

trimLeft ( whatToTrim )

trimRight ( whatToTrim )

trim ( whatToTrim )

trim spaces (default) or other characters from a string.


"   hi there__".trim( "_ " );


realEndNumber

better version of PathName:endNumber, with correct handling of file extensions


PathName("sounds/a11wlk01.wav").realEndNumber;

PathName("sounds/a11wlk01.wav").endNumber; // distro version - returns wrong number


realEndNumber_

sets the realEndNumber of a PathName

PathName("sounds/a11wlk01.wav").realEndNumber = 342;  // -> sounds/a11wlk342.wav

realNextName

better version of PathName:nextName. Also works on Strings

"sounds/a11wlk01.wav".realNextName;         // -> sounds/a11wlk2.wav

PathName( "sounds/a11wlk01.wav" ).nextName; // -> sounds/a11wlk01.wav1



deStandardizePath

make a user relative path from a standardized path


"~/Desktop/some file".standardizePath.postln.deStandardizePath; // proof of concept




graphic file processing


various image operations using the sips libary, built into MaxOSX. Might be useful to somebody :-)


resampleImage ( newMaxSize, newName )

resamples an image to the given newMaxSize (default 600). If newName == 'auto' (default) a new name is generated from the input path.


"/Library/Desktop Pictures/Black & White/Mojave.jpg".resampleImage( 200,"~/Desktop/resampled.jpg");


( // batch conversion

"~/Desktop/resized/".makeDir;

"/Library/Desktop Pictures/Black & White/".getPathsInDirectory( "jpg", 1, true ).do({ |path|

path.resampleImage( 200, "~/Desktop/resized/" ++ path.basename );

});

)


convertImage ( newFormat, newName )

converts an image to the given newFormat (default "jpg"). 


"~/Desktop/resampled.jpg".convertImage( "tiff" );


rotateImage ( degreesCW, newName )

rotates an image to the given degreesCW (default90). 


"~/Desktop/resampled.jpg".rotateImage( 45 );