Rename a file with an unknown name

Hi everyone,

It’s me (again), and i have a little bit of a hickup.

I’m trying to grab the newest file that get’s added to a certain folder. That in it self isn’t very hard, but the file name is randomized (always having 40 characters), and i’d like to rename it to a human readable name.

for example, it’s now called “84c9bcbea8cf8b0ebd96184e248a262b6f379e83”

And it’s neatly being copied to a folder of my choosing using this script, however, i’d like to change it’s name and extension. But that seems hard to do because it’s name is, as i said earlier, randomized. What would be the best way to aproach this? should I use this do shell script "ls -l"
to grab all file names and find out which one has 40 characters, or should i copy it to a empty temporary folder after which i rename all files (that one) in that folder?
At this point i got this:


on extractNow_()
        log "Generating path to Kuvva Folder.."
        set pathp1 to (path to desktop as text)
        set applescript's text item delimiters to ":"
        set pathp2 to (text item 1 thru 3 of pathp1 as text & ":")
        set pathp3 to pathp2 & "Library:Application Support:Wallpapers:Data:Wallpaper:"
        set pathp4 to (POSIX path of pathp3)
        set WallpaperPath to pathp4
        set ExtractPathPOSIX to POSIX path of ExtractPath
        log "Extraction running"
        do shell script "cp -a " & (quoted form of WallpaperPath) & ". \"" & extractPathPOSIX & "Extract\""
        tell application "Finder"
            set all_files to get items of (extractPath & "Extract:")
        
        repeat with ff in all_files
            set applescript's text item delimiters to ""
            if (count text items of ff) = "40" then
            set ff's name to "Extract"
            set ff's file type to "jpg"
            end
        end repeat
        end tell
        end extractNow_

If any of you could help, thank you very much for it!

Model: Macbook Pro
AppleScript: Latest (10.9.2)
Browser: Safari 537.74.9
Operating System: Mac OS X (10.8)

Hi. If you know the target is the newest file, I’d just change its name. If I were moving (or copying) it somewhere and wanted to perform another action, I’d alias it, so that it could be acted upon, wherever it got moved.


tell application "Finder" to set (folder ((path to library folder from user domain as text) & "Application Support:Wallpapers:Data:Wallpaper:")'s file 1)'s name to "Extract.jpg"

tell application "Finder" to (move ((move some file to trash) as alias) to desktop)'s name

hi again Xpresso,

when using ls you can pipe the output to grep and filter files the filer that only contains 40 lowercase characters a, b, c, d, e and f and symbols 0 thru 9.

set theFiles to every paragraph of (do shell script "ls " & quoted form of POSIX path of (path to desktop folder) & " | egrep '[0123456789abcdef]{40}' || true")
if theFiles is {""} then
    return "no files found"
end if

return theFiles

NOTE: I couldn’t find if the orignal file had an extension at all; I assumed it didn’t.

Alright, so i tried both codes, but think DJ’s worked best.

I had to tweak it a little bit, but it now works like a charm. The target folder is expected to have multiple randomized AND have files that have a human readable name.
So using (repeat with i in a) i set up a way to rename to so everyone can read it!

Thank you both very much!

Alright, so it worked perfectly. I used the mv command (in terminal) to rename the files.
However the application should run the extract subroutine on idle, but the entire on idle command is not being called at all. what should i do?

Did you save the script as “stay-open” application?

edit: and do you return a number at the end of the idle handler (number of seconds to delay).

Well, i recoded the script in xcode, so saving that way is now impossible (or i just don’t know how).

in the idle handler i let it return the number 1 (which should stand for 1 minute delay?)

If it’s an xcode project, assuming it’s AppleScriptObjC, you should use NSTimer instead because idle handlers doesn’t work in AppleScriptObjC projects.

Oh, alright, thank you!