在Delphi中获取MacOS应用程序支持文件夹
苹果说将存储应用程序状态的文件放在“应用程序支持”文件夹中,并“使用应用程序支持目录常量NSApplicationSupportDirectory” Delphi针对TPath的类方法有各种TPath.GetXXXX(包括TPath.GetLibraryPath),但我找不到返回应用程序支持文件夹的方法 如何在Firemonkey应用程序中获取应用程序支持文件夹?硬编码方法(您可能已经尝试过);由于应用程序支持文件夹位于库文件夹下:在Delphi中获取MacOS应用程序支持文件夹,macos,delphi,Macos,Delphi,苹果说将存储应用程序状态的文件放在“应用程序支持”文件夹中,并“使用应用程序支持目录常量NSApplicationSupportDirectory” Delphi针对TPath的类方法有各种TPath.GetXXXX(包括TPath.GetLibraryPath),但我找不到返回应用程序支持文件夹的方法 如何在Firemonkey应用程序中获取应用程序支持文件夹?硬编码方法(您可能已经尝试过);由于应用程序支持文件夹位于库文件夹下: uses System.IOUtils; function
uses System.IOUtils;
function GetApplicationSupportDir : string;
begin
Result := TPath.Combine(TPath.GetLibraryPath,'Application Support');
end;
或者,直接从iOS或OSX检索:(对于Delphi XE8及更高版本)
对于XE7,这可能适用于:
uses
Macapi.Helpers,
{$IFDEF iOS}
iOSapi.Foundation,
{$ENDIF}
{$IFDEF OSX}
Macapi.Foundation,
{$ENDIF}
System.IOUtils;
///////////////////////////// Added since XE7:
const
_PU = '_';
libFoundation = '/System/Library/Frameworks/Foundation.framework/Foundation';
type
NSUInteger = LongWord;
NSSearchPathDirectory = NSUInteger;
NSSearchPathDomainMask = NSUInteger;
function NSSearchPathForDirectoriesInDomains(directory: NSSearchPathDirectory; domainMask: NSSearchPathDomainMask;
expandTilde: Boolean): Pointer {NSArray}; cdecl;
external libFoundation name _PU + 'NSSearchPathForDirectoriesInDomains';
/////////////////////////////
function GetApplicationSupportDir : string;
var
Paths : NSArray;
Dir : NSString;
begin
// For "Application Support" under the User's Library directory:
Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True));
// For "Application Support" under the System Library directory:
//Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSSystemDomainMask, True));
Dir := TNSString.Wrap(Paths.objectAtIndex(0));
Result := NSStrToStr(Dir);
end;
这对您有帮助吗?。另外,如果您需要存储用户设置,请查看@AlbertoMiola不确定这与OP的问题有什么关系,Documents文件夹离Application Support文件夹不远。我确实希望直接从OSX获取它,因为字符串“Application Support”在本地化时可能会有所不同。我已尝试使用NSSearchPathForDIrTraceReStudiMyScript,但我在MaPIAPI .Fund或Delphi XE7的OSX的任何源文件中都找不到它。是否有不同的名称?或者这可能是在10.2中添加的吗?@ MiKeAtBooGuo--是的,它不是XE7;它是在XE8中添加的。我找出了需要什么,并为XE7创建了一个可能的解决方案。更新了答案。
uses
Macapi.Helpers,
{$IFDEF iOS}
iOSapi.Foundation,
{$ENDIF}
{$IFDEF OSX}
Macapi.Foundation,
{$ENDIF}
System.IOUtils;
///////////////////////////// Added since XE7:
const
_PU = '_';
libFoundation = '/System/Library/Frameworks/Foundation.framework/Foundation';
type
NSUInteger = LongWord;
NSSearchPathDirectory = NSUInteger;
NSSearchPathDomainMask = NSUInteger;
function NSSearchPathForDirectoriesInDomains(directory: NSSearchPathDirectory; domainMask: NSSearchPathDomainMask;
expandTilde: Boolean): Pointer {NSArray}; cdecl;
external libFoundation name _PU + 'NSSearchPathForDirectoriesInDomains';
/////////////////////////////
function GetApplicationSupportDir : string;
var
Paths : NSArray;
Dir : NSString;
begin
// For "Application Support" under the User's Library directory:
Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True));
// For "Application Support" under the System Library directory:
//Paths := TNSArray.Wrap(NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSSystemDomainMask, True));
Dir := TNSString.Wrap(Paths.objectAtIndex(0));
Result := NSStrToStr(Dir);
end;