Applescript - possibility?

Discussion in 'Mac Programming' started by DennisBlah, Apr 3, 2014.

  1. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Is it possible to retrieve the author name of an excel file, without actually opening the file ?

    Code:
    set thePath to (path to desktop as Unicode text) & "test.xlsx"
    get info for thePath as alias
    
    Is not really what I'm looking for.
    I want to retrieve the real Author which you can find in excel underneath:
    File->Properties : Author
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    There's a user name that appears in the meta data if you use 'mdls'. For example:

    do shell script "mdls /path/to.your/file.xls"

    …I'm not sure whether that corresponds to what you see in Excel though. Might be worth you doing some testing to see.

    Rob
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    Hello superscape, thanks for your reply. I have seen multiple things about mdls /path/to/file.extension

    But this always results me in into:
    Code:
    /Users/Dennis/Desktop/test.xlsx: could not find /Users/Dennis/Desktop/test.xlsx.
    
    Which I am really really REALLY sure it does exists and it is on this location.
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    That's odd. Have you tried dragging the file to your Terminal to make sure the path is correct?
     
  5. snorkelman, Apr 3, 2014
    Last edited: Apr 3, 2014

    snorkelman macrumors 6502a

    snorkelman

    Joined:
    Oct 25, 2010
    #5
    It does look like the item you're after is in there under kMDItemAuthors, value is returned in paragraph 2 of the mdls query, and yes there's no need to open the file (its last opened date wont change).

    Following should do what you're after (I've structured it to get all the words in para 2 rather than assuming folks always have a two word name).

    Code:
    set excelName to quoted form of POSIX path of (choose file)
    
    set mdlsResult to paragraph 2 of (do shell script "mdls " & excelName)
    
    set authorName to text (word 1) thru (word (count words of mdlsResult)) of mdlsResult
    
    return authorName
    ps is also good for Word PDF files etc
     
  6. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    You can also go straight after what you're looking for with something like this :

    Code:
    set excelName to quoted form of POSIX path of (choose file)
    set authorName to (do shell script "mdls -name kMDItemAuthors  -raw " & excelName & " | awk -F'\"' '{ getline; print $2; exit }'")
     
  7. snorkelman macrumors 6502a

    snorkelman

    Joined:
    Oct 25, 2010
    #7
    aye, or possibly go one stage further and reduce it to

    Code:
    set authorName to (do shell script "mdls -name kMDItemAuthors  -raw " & (quoted form of POSIX path of (choose file)) & " | awk -F'\"' '{ getline; print $2; exit }'")
     
  8. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #8
    You guys are amazing.
    However, with all these samples, I also get the error that the file is not been found.

    Did I noted that I'm running 10.9.2 ?
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Then you need to start with the simplest thing, find out if it works, and work up from that.

    Script 1:
    Code:
    set chosen to (choose file)
    
    What does this produce as a result when run in AppleScript Editor?
    Post the actual results, copied and pasted from the actual results window.

    Script 2:
    Code:
    set chosen to (choose file)
    get info for chosen
    Same questions here. Post the actual results.

    Script 3:
    Code:
    set chosen to (choose file)
    set pathname to Posix path of chosen
    Post the results.

    Script 4:
    Code:
    set chosen to (choose file)
    set pathname to quoted form of (Posix path of chosen)
    Choose a file with a space in its name. Post the results.

    Script 5:
    Code:
    set chosen to (choose file)
    set pathname to quoted form of (Posix path of chosen)
    set ls_info to do shell script "ls -ld " & pathname
    Choose a file with a space in its name. Post the results.


    Test the scripts in the order given. Post the actual results. Report any errors, posting the complete text of the actual error message.
     
  10. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #10
    Sorry for this late reply, I been really busy with a huge project over here.

    This ls -ld part of yours works well, unfortunally it's not getting the actual author of the excel file. I can retrieve this by opening the file and go through the menu's and stuff. But this is something I don't want to do, as I'm working on an 'robot' that has to determine if this file is authorized to be imported in our database. If so, then we get the actual content.

    Because I'm talking about 1000+ files a day, I don't want to have to open them fysically for determining the owner.

    I must be making my excuses to all of you guys. For some reason on my iMac mdls will never find any files from my desktop, however on my macbook it does. (I just tried it on my macbook)

    Again my excuse for this, I have experienced some more stupid trouble with this iMac. Thanks for all your help guys!
     
  11. andyadams macrumors newbie

    Joined:
    Feb 9, 2015
    #11
    I am a developer of Excel barcode addin.As for your question,i'd like to share some Excel tips with you,i hope it will help.
    The username that a person sets in Excel when first installing the software or when changing the general options for the program cannot be accessed via formula. Instead, you need to use a macro to access the information and then make it available to your worksheet. This is possible through the use of a user-defined function. Consider the following simple example:

    Code:
    Function GetUserName()
        GetUserName = Application.UserName
    End Function
    Note that the macro does nothing more than to access the UserName property of the Application object. You use this function in your worksheet in the following manner:
    Code:
    =GetUserName()
    With this simple formula in a cell, the username is displayed in the cell.
     

Share This Page