Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

opening a .jpeg-file in a folder with Applescript

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

openurl_path

if not (exists url_path) then

display dialog "not found"

else

display dialog "path=" & url_path

opendocument 1

--error number -1719 from document 1

set name of document 1 to album_title

display dialog & name of document 1

closedocument 1 savingyes

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

Posted on Feb 20, 2013 4:05 PM

Reply
Question marked as Best reply

Posted on Feb 20, 2013 5:10 PM

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


opendocument 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 fileurl_path to album_title

display dialog url_path & " was renamed to " & album_title

end if

end try

end tell

9 replies
Question marked as Best reply

Feb 20, 2013 5:10 PM in response to A.P.M. Wülfinghoff

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


opendocument 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 fileurl_path to album_title

display dialog url_path & " was renamed to " & album_title

end if

end try

end tell

Feb 21, 2013 4:30 AM in response to Camelot

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.

Feb 21, 2013 8:44 AM in response to A.P.M. Wülfinghoff

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.









Feb 21, 2013 9:42 AM in response to HD

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"


openurl_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.

Feb 21, 2013 10:35 AM in response to A.P.M. Wülfinghoff

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.

Feb 21, 2013 11:03 AM in response to A.P.M. Wülfinghoff

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 filefile_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

Feb 21, 2013 11:11 AM in response to Camelot

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

Feb 21, 2013 11:23 AM in response to Camelot

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"

opening a .jpeg-file in a folder with Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.