Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder and inside this folder there's a bunch of subfolders. Using AppleScript I wish to store the pathnames of these subfolders into an array.

Here's my problem: each pathname containing " " (spaces), I want to replace with pathnames containing UNIX-friendly "" notation (backslash followed by a space; as in /my/fancy\ path/).

As you can see, I'm only half-way through my goal here. I've spent a good night's worth of laborious attempts, fooling around with replace_chars subroutines, do shell script-with-sed combos and who knows what. No dice.

tell application "Finder"

    set myRepos to name of folders of folder ("/Users/hced/Dropbox/GitHub/" as POSIX file)
    --> {"320andup", "Baseline.js (jQuery & vanilla JS version)", "Bootstrap (HTML, CSS, and JS toolkit from Twitter)", "Chirp.js (Tweets on your website, simply)", "Coordino"}

end tell

Edit: a valid path example would be /Users/hced/Dropbox/GitHub/A\ folder\ named\ with\ spaces

share|improve this question
    
Are you trying to escape the paths to pass back into a shell command? –  adayzdone Nov 14 '12 at 1:42
    
adayzdone: yes. I want to iterate over the items in the array via a macro in Keyboard Maestro (it's a bit complicated to explain the full story). Also, some part of the question is just because I'm curious. –  hced Nov 14 '12 at 17:10
    
Actually – and this is probably not the (or a) recommended way of doing things git-wise, but – my end goal is to parse my GitHub folder for subfolders one level down and then mass-git fetch in all of the repos. Brutal, yes, but I didn't find a way of doing it within GitHub.app (Mac), and all the projects are just things I'm not touching (forking) myself, I just want the newest of everything. The choice of AppleScript in this case is because I will store the contents of myRepos as variable in Keyboard Maestro... Sounds like a mess? Indeed, but there's the story. –  hced Nov 14 '12 at 17:23

3 Answers 3

up vote 2 down vote accepted

Reading your question, I'm not sure if you are able to use quoted path names instead of escaping the spaces...

tell application "Finder"
        set folderGitHub to folder ("/Users/hced/Dropbox/GitHub/" as POSIX file)

        set listFolders to (every folder in folderGitHub as alias list)
end tell

set listPosixPathsQuoted to {}

repeat with aliasFolder in listFolders
        set listPosixPathsQuoted to listPosixPathsQuoted & {quoted form of POSIX path of aliasFolder}
end repeat
share|improve this answer

This should do it:

tell application "Finder"
    set myRepos to name of folders of folder ("/Users/hced/Dropbox/GitHub/" as POSIX file)
end tell

repeat with theIndex from 1 to number of items in myRepos
    set item theIndex in myRepos to replace_chars(item theIndex in myRepos, " ", "\\ ")
end repeat

return myRepos

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

This takes the folder names, which you got already, and loops over each item in the list replacing every space character with \. Note that the backslash needs to be escaped, and that AppleScript Editor displays the strings as including the double backslash. However you can verify that they're properly escaped with a single backslash by doing set the clipboard to item 2 of myRepos and pasting the resulting text into a text editor — it's just a quirk of AppleScript Editor.

The replace_chars function is a fairly standard boilerplate. I copied it from Mac OS X Automation.

share|improve this answer
    
Similarly to adayzdone's script above, this outputs results that doesn't conform to valid UNIX paths (with [:space:]'s replaced by (backslash + space). Is it impossible to achieve \ with AppleScript? Here's sample output of your script: {"320andup", "Baseline.js\\ (jQuery\\ &\\ vanilla\\ JS\\ version)", "Bootstrap\\ (HTML,\\ CSS,\\ and\\ JS\\ toolkit\\ from\\ Twitter)", "Chirp.js\\ (Tweets\\ on\\ your\\ website,\\ simply)", "Coordino"} –  hced Nov 14 '12 at 18:01
1  
As I noted, that's just the output as AppleScript Editor displays it. The actual data is in the format you specified. You can test this by adding set the clipboard to myRepos as string on a line before the return command, then paste into a text editor. You'll wee the spaces are escaped with single backslashes. If you need specific help working with the output in another way, add that to your question and I can amend my answer. –  robmathers Nov 14 '12 at 18:12

Trash Man's handler posted on MacScripter will escape all of the problem characters...

set myFolder to quoted form of "/Users/hced/Dropbox/GitHub/"

    set theFolders to every paragraph of (do shell script "find " & myFolder & " -type d -depth 1 ")
    set escFolders to {}

    repeat with aFolder in theFolders
        set end of escFolders to escape_string(aFolder as text)
    end repeat

    on escape_string(input_string)
    set output_string to ""
    set escapable_characters to " !#^$%&*?()={}[]'`~|;<>\"\\"
    repeat with chr in input_string
        if (escapable_characters contains chr) then
            set output_string to output_string & "\\" -- This actually adds ONE \ to the string.
        else if (chr is equal to "/") then
            set output_string to output_string & ":" -- Swap file system delimiters
        end if
        set output_string to output_string & chr
    end repeat
    return output_string as text
end escape_string
share|improve this answer
    
This doesn't output the path format I'm looking for. The script outputs e.g. /Users/hced/Dropbox/GitHub//Baseline.js (jQuery & vanilla JS version) I'm struggling with how to actually output in AppleScript strings... Seems impossible. –  hced Nov 14 '12 at 17:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.