Pregunta

Me puede obtener los nombres de todos los archivos en una carpeta al hacer esto:

tell application "Finder"
    set myFiles to name of every file of somePath
end tell

¿Cómo puedo cambiar las cuerdas en myFiles de modo que no incluyen la extensión de archivo?

Podría por ejemplo {"foo.mov", "bar.mov"} conseguir, pero me gustaría tener {"foo", "bar"}.


Solución actual

Sobre la base de la respuesta aceptada me ocurrió con el código de abajo. Déjame saber si se puede hacer más limpia o más eficiente de alguna manera.

-- Gets a list of filenames from the
on filenames from _folder

    -- Get filenames and extensions
    tell application "Finder"
        set _filenames to name of every file of _folder
        set _extensions to name extension of every file of _folder
    end tell

    -- Collect names (filename - dot and extension)
    set _names to {}
    repeat with n from 1 to count of _filenames

        set _filename to item n of _filenames
        set _extension to item n of _extensions

        if _extension is not "" then
            set _length to (count of _filename) - (count of _extension) - 1
            set end of _names to text 1 thru _length of _filename
        else
            set end of _names to _filename
        end if

    end repeat

    -- Done
    return _names
end filenames

-- Example usage
return filenames from (path to desktop)
¿Fue útil?

Solución

Aquí hay un guión completo que hace lo que quería. Me resistía a publicar originalmente porque pensé que había algunas sencillas de una sola línea, que alguien podría ofrecer como una solución. Esperemos que esta solución no es forma de hacer las cosas un Rube Goldberg .

El diccionario Buscador tiene un extensión de nombre propiedad para que pueda hacer algo como:

tell application "Finder"
   set myFiles to name extension of file 1 of (path to desktop)
end tell

Así que lo anterior le conseguirá sólo la extensión del primer archivo en el escritorio del usuario. Parece que habría una función simple para obtener la (nombre de la base - extensión). Pero no he encontrado un solo

Aquí está la secuencia de comandos para obtener sólo los nombres de archivo sin extensión para cada archivo en un directorio completo:

set filesFound to {}
set filesFound2 to {}
set nextItem to 1

tell application "Finder"
  set myFiles to name of every file of (path to desktop) --change path to whatever path you want   
end tell

--loop used for populating list filesFound with all filenames found (name + extension)
repeat with i in myFiles
  set end of filesFound to (item nextItem of myFiles)
  set nextItem to (nextItem + 1)
end repeat

set nextItem to 1 --reset counter to 1

--loop used for pulling each filename from list filesFound and then strip the extension   
--from filename and populate a new list called filesFound2
repeat with i in filesFound
  set myFile2 to item nextItem of filesFound
  set myFile3 to text 1 thru ((offset of "." in myFile2) - 1) of myFile2
  set end of filesFound2 to myFile3
  set nextItem to (nextItem + 1)
end repeat

return filesFound2

A pesar de la secuencia de comandos funciona si alguien conoce una manera más sencilla de hacer lo que el PO quería publicarlo porque todavía la sensación de que no debe haber una manera más sencilla de hacerlo. Tal vez hay una adición de secuencias de comandos que facilita esto también. Alguien sabe?

Otros consejos

http://www.macosxautomation.com/applescript/sbrt/index.html :

on remove_extension(this_name)
  if this_name contains "." then
    set this_name to ¬
    (the reverse of every character of this_name) as string
    set x to the offset of "." in this_name
    set this_name to (text (x + 1) thru -1 of this_name)
    set this_name to (the reverse of every character of this_name) as string
  end if
  return this_name
end remove_extension

He aquí un método para obtener applescriptish idea de Buscador de lo que el nombre del archivo es despojado:

set extension hidden of thisFile to true
set thisName to displayed name of thisFile
-- display dialog "hey"
set extension hidden of thisFile to false

Una sola línea manera de hacerlo, sin Finder, no hay eventos del sistema. Por lo tanto más eficiente y más rápido. Efecto secundario (podría ser bueno o malo): un nombre de archivo que termina en "" tendrá este carácter despojado. El uso de "marcha atrás de cada personaje" hace que funciona si el nombre que más de un período.

set aName to text 1 thru ((aName's length) - (offset of "." in ¬
    (the reverse of every character of aName) as text)) of aName

La solución como un controlador para procesar una lista de nombres:

on RemoveNameExt(aList)
    set CleanedList to {}
    repeat with aName in aList
        set the end of CleanedList to text 1 thru ((aName's length) - (offset of ¬
            "." in (the reverse of every character of aName) as text)) of aName
    end repeat
    return CleanedList
end RemoveNameExt

No sé cómo quitar las extensiones cuando se utiliza el "todos los archivos" la sintaxis, pero si no te importa un bucle (loop no se muestra en el ejemplo) a través de cada archivo, entonces esto va a funcionar:

tell application "Finder"
  set myFile to name of file 1 of somePath
  set myFile2 to text 1 thru ((offset of "." in myFile) - 1) of myFile
end tell

Dentro de un decir "Buscador" bloquear esta nombres de archivo recoge despojados de la extensión en myNames:

repeat with f in myFiles
    set myNames's end to ¬
        (f's name as text)'s text 1 thru -(((f's name extension as text)'s length) + 2)
end repeat
scroll top