9 Replies Latest reply: Feb 21, 2013 2:06 PM by A.P.M. Wülfinghoff
A.P.M. Wülfinghoff Level 1 Level 1 (5 points)

I am trying to change the names of .jpeg files into the names of the corresponding music albums in a folder.

As far as my knowledge of Applescript goes, the following script should do the job, but it does not open the .jpeg file, i.e. document 1

 

tell application "FileMaker Pro"

  activate

          set url_path to cell "bestandspad_cover" of current record

          set album_title to cell "album title" of current record & ".jpeg"

end tell

 

----------------------------------------------------

tell application "System Events"

               try

            open url_path

                              if not (exists url_path) then

                                             display dialog "not found"

                              else

                                             display dialog "path=" & url_path

                 open document 1

--error number -1719 from document 1

                                             set name of document 1 to album_title

                  display dialog & name of document 1

                 close document 1 saving yes

                              end if

               end try

end tell

-------------------------------------------------

 

I work in an FMP environment, but that should not bother you. I am using the Applescript editor to simulate the actual situation.

I tried 'open', 'get' and 'getURL', to no avail; in my experience Applescript sometimes requires illogical, or say imaginative solutions.

Do you see one?


iMac, OS X Mountain Lion (10.8.2), FileMaker Pro 11; iTunes 11.0.1 (12
  • 1. Re: opening a .jpeg-file in a folder with Applescript
    Camelot Level 8 Level 8 (45,790 points)

    Your script is doomed to failure, not least because of the line:

     

                     open document 1


    This is in a 'tell application "System Events"' block, but System Events isn't a document-based application, so it's going to fail. The problem is that your script is within a 'try/end try' block so any errors are suppressed - in other words the script fails silently and never gets to the parts where you would rename the file.

     

    At the very, very least you need to refer to url_path (the data that came out of Filemaker Pro), not 'document 1'.

    You also need to validate that url_path exists and is in the right format to reference a file - without seeing sample data it's not clear if this is a specific URL (as the name implies, e.g. "file:///such/as/this.jpg"), or a UNIX-style path (/such/as/this.jpg) or a Mac-style path (such:as:this.jpg)

     

    At the very least you need to coerce the data to a file reference since the data coming out of Filemaker is almost certainly just text (so your text 'exists url_path') will always return true... yup, that text exists!)

     

    Finally, you're opening the file, then trying to change its name. You should change its name before opening it (assuming you need to open it at all).

     

    Try something like:

     

    tell application "System Events"

              try

                        if not (exists file url_path) then

                                  display dialog url_path & " not found"

                        else

                                  display dialog "path = " & url_path

                                  set name of file url_path to album_title

                                  display dialog url_path & " was renamed to " & album_title

                        end if

              end try

    end tell


  • 2. Re: opening a .jpeg-file in a folder with Applescript
    A.P.M. Wülfinghoff Level 1 Level 1 (5 points)

    Thanks for your comment.

    I am afraid I have no deeper insight into the workings of Applescript.

    I modeled this program after an earlier example running QuickTime Player, which is an entirely different application.

     

    I used your suggestion, but without success.

     

    Maybe I should better explain my objective.

    The url_path  is a specific URL like "file://Macintosh HD/Users/ etc". It leads you to a specific file, in this case a .jpeg file, which has a name, which of course is imbedded in the url_path, but not the same.

    I want to change this name and I know that this operation will invalidate the url_path, but that is no problem.

    Subsequently I want the file to be placed in the original folder under the new name.

     

    From the error message I get the impression that the program now tries to rename the url_path, which is not meant; the name of the file to which the url_path leads, should be renamed.

  • 3. Re: opening a .jpeg-file in a folder with Applescript
    HD Level 4 Level 4 (3,240 points)

    What happens If you run this script:

     

    tell application "FileMaker Pro"

      activate

              set url_path to cell "bestandspad_cover" of current record

              set album_title to cell "album title" of current record & ".jpeg"

    end tell

    display dialog url_path

    display dialog album_title

    end tell

     

    If it displays the text of the url_path followed by album_title, could you post exact examples of each?

     

    I'm not familiar with FMP but you may need to

     

    set url_path to content of cell "bestandspad_cover" of current record

     

    Or maybe contents, or text, or something else altogether.

     

     

     

     

     

     

     

     


  • 4. Re: opening a .jpeg-file in a folder with Applescript
    A.P.M. Wülfinghoff Level 1 Level 1 (5 points)

    Thanks for thinking with me.

    The problem lies not with FMP; hereafter the Applescript in the editor; I left out the displays, but the content is given in the next line.

    ==========================

    tell application "FileMaker Pro"

      activate

              set url_path to cell "bestandspad_cover" of current record

      --"file://Macintosh HD/Users/apmwulfinghoff/Documents/POPMUZIEK/ALBUM COVERS POP/325.jpeg"

              set album_title to cell "album title" of current record & ".jpeg"

      --First Ladies Of R&B.jpeg

    end tell

    ----------------------------------------------------

    tell application "System Events"

      open url_path

              if not (exists url_path) then

      beep

                        display dialog "not found"

              else

      display dialog (get album_title)

                        tell application "Finder"

      activate

                                  get document file

                                  get name of document file

      --name of file is "325.jpeg" referred to by url_path

    --here I get persistent error messages

      display dialog & name

                                  set name to album_title

      --set name of document file "325.jpeg" of folder "ALBUM COVERS POP" of folder "POPMUZIEK" of folder "Documents" of folder "apmwulfinghoff" of folder "Users" of startup disk to "First Ladies Of R&B.jpeg"

      -- previous line is action as recorded in editor; all info is here available except "325.jpeg" separately

                        end tell

              end if

      --save document file

      --close folder saving yes

    end tell

    ================================

    The real problem for me is to obtain the name of the document file; I can't figure out why I cannot succeed.

  • 5. Re: opening a .jpeg-file in a folder with Applescript
    HD Level 4 Level 4 (3,240 points)

    I think the problem is with the URLs in your database, which don't match URLs in the file system.

     

    Try this, for example:

     

    tell application "Finder"

    set the_URL to URL of file (choose file)

    end tell

     

    -- "file://localhost/Users/myusername/Desktop/Photo/Clifton%20Bridge.jpg"

     

    You'll notice that this refers to localhost, not "Macintosh HD", and it adds URL encoding (eg %20 for a space) as well. So when you pass your database URLs to the Finder, or System Events, they don't match up to the actual URLs of the files on your disk.

     

    I think you'll need to parse the database references in the script before you pass them to the Finder, or modify the database itself so that the refererences are correct file system URLs for the Mac.

  • 6. Re: opening a .jpeg-file in a folder with Applescript
    Camelot Level 8 Level 8 (45,790 points)

    HD is mostly right - the problem lies in the fact you're using URLs in your database.

    The spaces aren't so much the problem, since the OS will actually handle that one, but you cannot just 'open' a URL. The Open command specifically expects a file reference of some kind, not a URL.

     

    To 'open' a URL you have to use the 'open location' command:

     

    open location "url_path"

     

    but that's still irrelevant for what you're trying to do - there is NO requirement to OPEN the file in order to rename it.

     

    The problem you have is in mapping the URL to a specific file reference that the Finder can interpret. Off hand I couldn't see a direct approach, but you might be able to get there with a little hacking:

     

    set url_path to "file://Macintosh HD/Users/apmwulfinghoff/Documents/POPMUZIEK/ALBUM COVERS POP/325.jpeg"

    set file_path to characters 7 through end of url_path as text

    set the_file to POSIX file file_path

     

    In this example, url_path is set to a file URL (which could easily come from your database).

    On the second line, file_path strips off the leading 'file:/' characters which leaves a more traditional UNIX-style path that can then be coerced to a normal file reference (line 3).

    Once you have a file reference it's easy to rename it:

     

    tell application "Finder"

              set name of file the_file to "woohoo.jpg"

    end tell

  • 7. Re: opening a .jpeg-file in a folder with Applescript
    HD Level 4 Level 4 (3,240 points)

    Or possibly (not my replaceText handler)

     

    on replaceText(find, replace, someText)

              set prevTIDs to text item delimiters of AppleScript

              set text item delimiters of AppleScript to find

              set someText to text items of someText

              set text item delimiters of AppleScript to replace

              set someText to "" & someText

              set text item delimiters of AppleScript to prevTIDs

              return someText

    end replaceText

     

     

     

    tell application "FileMaker Pro"

    activate

         set url_path to cell "bestandspad_cover" of current record

    set album_title to cell "album title" of current record & ".jpeg"

    end tell

     

    set half_bad_url to my replaceText("file://", "", url_path)

    set the_alias to my replaceText("/", ":", half_bad_url) as alias

    tell application "Finder" to set name of the_alias to album_title

  • 8. Re: opening a .jpeg-file in a folder with Applescript
    twtwtw Level 5 Level 5 (4,690 points)

    actually, that's not quite right: unix paths don't include the hard drive name.  besides, I think it's more stable to use text item delimiters here:

     

    set url_path to "file://Macintosh HD/Users/apmwulfinghoff/Documents/POPMUZIEK/ALBUM COVERS POP/325.jpeg"

     

    set {oldTID, my text item delimiters} to {my text item delimiters, "/"}

    -- get pieces of path, stripping off the 'file://' bit

    set url_bits to (text items 3 thru -1 of url_path)

    --delete the hard drive name

    set item 1 of url_bits to ""

    -- recombine into a normal posix path

    set posix_path to url_bits as text

    set my text item delimiters to oldTID

     

    -- posix_path = /Users/apmwulfinghoff/Documents/POPMUZIEK/ALBUM COVERS POP/325.jpeg"

  • 9. Re: opening a .jpeg-file in a folder with Applescript
    A.P.M. Wülfinghoff Level 1 Level 1 (5 points)

    Thank you all for your contributions.

    The simple solution from HD

     

    "tell application "Finder" to set name of the_alias to album_title"

     

    was most attractive. I know I should make a thorough study of Applescript, but with your help I have been able to postpone that till now.

     

    Thanks again for your patience.