Hi!
Do you know any resource to learn some basics about shell scripting, but absolutely oriented to an applescripter & “do shell script” command? (free/ware)
Anything such as “shell scripting as a replacement for Finder and file manipulation” would be great.
Some routines such as “duplicate”, “set name of”, “delete” or “every file of entire contents of x whose name ends with ‘xxx’”, use of variables, multiline shell scripts, regular expressions usage, unix wildcards, simple control structures… Would be so useful, specially these days when scripting the Finder is a terrible slow job… :?:
I don’t know of any such resource but I haven’t read all of the books that are available. Just this morning, I found a web site which has a whole bunch of OS X man pages online. This, at least, allows us to easily see what’s available.
Alphabetic: http://www.osxfaq.com/MAN/Index/A.ws
By Section: http://www.osxfaq.com/man/index/1.ws
Here’s a sample command that lets you rename a file or folder. I believe I handled difficult characters, but haven’t thoroughly tested. Anyone see a bug?
-- SAMPLE USAGE here, handler below
set newFilePath to shellRename(file "YourDrive:Users:yourname:Desktop:TEMP:some'dir:bob", "person's ploy")
-- note: the originalObject can be a file or folder, and can be a file, an alias, or just a string path
tell application "Finder" to open alias newFilePath
return newFilePath
on shellRename(originalObject, newName)
-- version 1.0, Daniel A. Shockley
if (originalObject as string) ends with ":" then -- it is a folder itself
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set parentFolder to (text items 1 thru -3 of (originalObject as string)) as string
set AppleScript's text item delimiters to od
else -- it is just a file
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set parentFolder to (text items 1 thru -2 of (originalObject as string)) as string
set AppleScript's text item delimiters to od
end if
-- just to make do shell script line easier to read
set dirPath to quoted form of POSIX path of parentFolder
set objPath to quoted form of POSIX path of originalObject
try
-- move to the directory, 'mv' the file to the new name
do shell script "cd " & dirPath & "; mv " & objPath & " ./" & quoted form of newName
return (parentFolder & ":" & newName) -- return a string: could also use alias
-- CANNOT return a file, since there seems to be a delay in noticing the new name, and it errors
on error errMsg number errNum
error "shellRename FAILED: " & errMsg number errNum
end try
end shellRename