我相信您必须使用 Apple Script 才能在 Mac 上做得更好。以下代码允许用户选择从函数中作为数组返回的文本文件。您只需修改 Apple 脚本以返回其他文件类型并选择目录,我将把它留给您。
调用该函数并显示包含所有文件的消息框的代码:
Sub GetTextFilesOnMac()
Dim vFileName As Variant
'Call the function to return the files
vFileName = Select_File_Or_Files_Mac
'If it's empty then the user cancelled
If IsEmpty(vFileName) Then Exit Sub
'Loop through all the files specified
For ii = LBound(vFileName) To UBound(vFileName)
MsgBox vFileName(ii)
Next ii
End Sub
执行 Apple Script 的功能:
Function Select_File_Or_Files_Mac() As Variant
'Uses AppleScript to select files on a Mac
Dim MyPath As String, MyScript As String, MyFiles As String, MySplit As Variant
'Get the documents folder as a default
On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")
'Set up the Apple Script to look for text files
MyScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
"set theFiles to (choose file of type " & " {""public.TEXT""} " & _
"with prompt ""Please select a file or files"" default location alias """ & _
MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
"set applescript's text item delimiters to """" " & vbNewLine & _
"return theFiles"
'Run the Apple Script
MyFiles = MacScript(MyScript)
On Error GoTo 0
'If there are multiple files, split it into an array and return the results
If MyFiles <> "" Then
MySplit = Split(MyFiles, ",")
Select_File_Or_Files_Mac = MySplit
End If
End Function
最后,指定不同的文件类型可能会有点麻烦,如果您只想指定 Word 文档,然后替换public.TEXT
为com.microsoft.word.doc
,但是这不允许.docx
or.docm
文件。您需要分别为这些使用org.openxmlformats.wordprocessingml.document
和。org.openxmlformats.wordprocessingml.document.macroenabled
有关这些的更多信息,请参阅:https ://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html