2013-11-09 90 views
0

我试图做一个命令来隐藏和显示我的桌面上的文件夹,这是到目前为止我的代码中的AppleScript:制作文件夹隐藏和取消隐藏

on run 
    if "chflags hidden ~/Desktop/*" then 
     do shell script "chflags nohidden ~/Desktop/*" 
    else 
     do shell script "chflags hidden ~/Desktop/*" 
    end if 
end run 

u能请查找问题,并帮助 谢谢

回答

0

该命令似乎按预期工作。我用我的桌面上的一个文件夹测试它,所以

chflags hidden ~/Desktop/testDir/* 
chflags nohidden ~/Desktop/testDir/* 

完成这项工作。

您的if语句不起作用。

if "chflags hidden ~/Desktop/*" then 

那什么都不做。即使你想补充缺少的“做shell脚本”:

if (do shell script "chflags hidden ~/Desktop/testDir/*") then 

这实际上会掩盖一切(你不想在这一点),它没有返回,并产生一个AppleScript的错误。

所以你必须寻找另一种方法来检查隐藏状态。

下面是一个代码示例这样做:

tell application "System Events" 
    set filePath to file (((path to desktop) as text) & "myReferenceFile.txt") 
end tell 

set this_info to info for filePath 
if visible of this_info is true then 
    log "VISIBLE" 
else 
    log "INVISIBLE" 
end if 

如果你有一个参考文件,你可以使用这条道路,以检查它是否隐藏或不。

+1

THX的家伙:d要去看看他们的工作! :d – user2826998

0

你可以用这样的切换标志:

property hideFolders : true 

if hideFolders then 
    do shell script "find ~/Desktop/* -type d -maxdepth 1 -exec chflags hidden {} +" 
    set hideFolders to false 
else 
    do shell script "find ~/Desktop/* -type d -maxdepth 1 -exec chflags nohidden {} +" 
    set hideFolders to true 
end if