【问题标题】:Restrict file types on droplet限制 Droplet 上的文件类型
【发布时间】:2023-03-27 03:58:01
【问题描述】:

这个问题不限于懂AppleScript的人,如果你做Cocoa等,你应该知道这个答案:

我在 AppleScript 中制作了一个压缩 JavaScript 文件的 droplet,所以很明显,我只希望 JavaScript 文件被允许进入脚本。有什么想法吗?

非常感谢。

【问题讨论】:

    标签: javascript applet applescript restrict


    【解决方案1】:
    property kJavascriptExtension : "js"
    property pValidFileList : {}
    
    on open of theFiles -- Executed when files are dropped on the script
    
        set fileCount to (get count of items in theFiles)
    
        repeat with thisFile from 1 to fileCount
            set theFile to item thisFile of theFiles
            set theFileAlias to theFile as alias
    
            tell application "Finder"
                set fileInfo to info for theFileAlias
                set fileName to name of fileInfo
            end tell
    
            set javascriptFileFound to isJavascriptFile(fileName) of me
    
            if (javascriptFileFound) then
                set end of pValidFileList to theFile
            end if
        end repeat
    
        display dialog "pValidFileList = " & pValidFileList
        -- do something with your files here
    end open
    
    on isJavascriptFile(theFilename) -- (theFilename as string) as boolean
        set AppleScript's text item delimiters to "."
        set fileNameList to every text item of theFilename
        set AppleScript's text item delimiters to ""
    
        try
            set theFileExtension to item 2 of fileNameList as string
        on error
            return false
        end try
    
        if theFileExtension is kJavascriptExtension then
            return true
        end if
    
        return false
    end isJavascriptFile
    

    【讨论】:

    • 这太棒了!我期待某种 Info.plist 属性,但这似乎要简单得多。
    猜你喜欢
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多