有办法能拿到 Mac 自带 preview 打开的 pdf 的进度吗?

2020-11-26 15:01:00 +08:00
 Jooooooooo

搜了一下没搜着, 很可能是因为关键词不对

需求是多个 Mac 之间同步 pdf 的进度

找了很多方法都不行, 苹果自带 Books 不好用, Kindle 也不好用

我想着如果我能拿到本机是怎么保存 pdf 读取进度的, 我可以自己写个同步的小脚本自己用

599 次点击
所在节点    问与答
2 条回复
alaysh
2020-11-27 17:29:47 +08:00
~/Library//Containers/com.apple.Preview/Data/Library/Preferences/com.apple.Preview.ViewState.plist
找到文件对应 Key 的 Data,用 plist 格式解开,elementIndex 就是所需的值。

#import <Foundation/Foundation.h>
#include <sys/stat.h>

NSString *persistentIdForFileURL(NSURL *url) {
NSString *persistentId;

id value;
NSError *error;
BOOL res = [url getResourceValue:&value forKey:NSURLVolumeUUIDStringKey error:&error];
if (res) {
struct stat statBuf;
int res = stat(url.fileSystemRepresentation, &statBuf);
if (res == 0) {
persistentId = [NSString stringWithFormat:@"%@.%llu", value, statBuf.st_ino];
}
else {
NSLog(@"stat error: %d", res);
}
}
else {
NSLog(@"getResourceValue error:%@", error);
}
return persistentId;
}

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSURL *url = [NSURL URLWithString:@"file:///tmp/1.pdf"];
NSString *persistentId = persistentIdForFileURL(url);

NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true)[0];
path = [path stringByAppendingString:@"/Containers/com.apple.Preview/Data/Library/Preferences/com.apple.Preview.ViewState.plist"];
NSData *data = [NSData dataWithContentsOfFile:path];

NSError *error;
NSDictionary *propertyList = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:0 error:&error];
propertyList = propertyList[persistentId];
if (propertyList) {
data = propertyList[@"Data"];
propertyList = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:0 error:&error];
if (propertyList) {
NSLog(@"propertyList: %@", propertyList);
}
else {
NSLog(@"propertyListWithData error:%@", error);
}
}
else {
if (error) {
NSLog(@"propertyListWithData error:%@", error);
}
NSLog(@"propertyList for %@ is null", url);
}
}
return 0;
}
Jooooooooo
2020-11-27 17:34:25 +08:00
@alaysh 感谢. 我看看.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/729500

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX