【问题标题】:Extracting Gif Files and putting them in a folder within the contents of the app提取 Gif 文件并将它们放在应用程序内容中的文件夹中
【发布时间】:2023-03-19 22:10:01
【问题描述】:

我正在制作的 AppleScript 从 gif 文件中提取帧,并将单个图像放入应用程序内容内的文件夹中。然后它将桌面背景快速更改为文件夹内的图像,从而为您提供墙纸的 gif。但是,我不知道如何将 gif 文件提取到应用程序中的文件夹中。这是我目前所拥有的:

on delay duration
set endTime to (current date) + duration
repeat while (current date) is less than endTime
    tell AppleScript to delay duration
end repeat
end delay
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select  GIF"
tell application "System Events" to set gifFileName to name of gifFiles
set dest to quoted form of POSIX path of (path to me) & "Contents:Resources:Gif"

set pScript to quoted form of "from AppKit import NSApplication, NSImage, NSImageCurrentFrame, NSGIFFileType; import sys, os
tName=os.path.basename(sys.argv[1])
dir=sys.argv[2]
app=NSApplication.sharedApplication() 
img=NSImage.alloc().initWithContentsOfFile_(sys.argv[1])
if img:
     gifRep=img.representations()[0]
 frames=gifRep.valueForProperty_('NSImageFrameCount')
 if frames:
     for i in range(frames.intValue()):
         gifRep.setProperty_withValue_(NSImageCurrentFrame, i)
         gifRep.representationUsingType_properties_(NSGIFFileType, None).writeToFile_atomically_(dir + tName + ' ' + str(i + 1).zfill(2) + '.gif', True)
     print (i + 1)"

repeat with f in gifFiles
set numberOfExtractedGIFs to (do shell script "/usr/bin/python -c " & pScript & " " & (quoted form of POSIX path of f) & " " & dest) as integer
end repeat
repeat
try
    set desktop_image to (path to me as string) & "Contents:Resources:Gif:" & gifFileName & " 01.gif"
    tell application "Finder" to set the desktop picture to desktop_image
end try
delay 0.05
try
    set desktop_image to (path to me as string) & "Contents:Resources:Gif:" & gifFileName & " 02.gif"
    tell application "Finder" to set the desktop picture to desktop_image
end try
delay 0.05
try
    set desktop_image to (path to me as string) & "Contents:Resources:Gif:" & gifFileName & " 03.gif"
    tell application "Finder" to set the desktop picture to desktop_image
end try
delay 0.05
try
    set desktop_image to (path to me as string) & "Contents:Resources:Gif:" & gifFileName & " 04.gif"
    tell application "Finder" to set the desktop picture to desktop_image
end try
delay 0.05
try
    set desktop_image to (path to me as string) & "Contents:Resources:Gif:" & gifFileName & " 05.gif"
    tell application "Finder" to set the desktop picture to desktop_image
end try
end repeat

【问题讨论】:

    标签: applescript


    【解决方案1】:

    脚本中存在一些问题。

    • choose file 返回单个文件。重复循环 repeat with f in gifFiles 会破坏脚本。
    • 目标文件夹的正确 POSIX 路径是
      set dest to quoted form of (POSIX path of (path to me) & "Contents/Resources/Gif")
    • 构成文件路径时必须加斜线

    试试这个,脚本假定Resources文件夹中的文件夹Gif存在。

    set gifFile to choose file of type "com.compuserve.gif" with prompt "Select  GIF"
    tell application "System Events" to set gifFileName to name of gifFile
    set dest to quoted form of (POSIX path of (path to me) & "Contents/Resources/Gif")
    
    set pScript to quoted form of "from AppKit import NSApplication, NSImage, NSImageCurrentFrame, NSGIFFileType; import sys, os
    tName=os.path.basename(sys.argv[1])
    dir=sys.argv[2]
    app=NSApplication.sharedApplication() 
    img=NSImage.alloc().initWithContentsOfFile_(sys.argv[1])
    if img:
     gifRep=img.representations()[0]
     frames=gifRep.valueForProperty_('NSImageFrameCount')
     if frames:
      for i in range(frames.intValue()):
       gifRep.setProperty_withValue_(NSImageCurrentFrame, i)
       gifRep.representationUsingType_properties_(NSGIFFileType, None).writeToFile_atomically_(dir + '/' + tName + ' ' + str(i + 1).zfill(2) + '.gif', True)
      print (i + 1)"
    
    
    do shell script "/usr/bin/python -c " & pScript & " " & (quoted form of POSIX path of gifFile) & " " & dest
    tell application "Finder"
        set extractedGiFFiles to files of folder ((path to me as text) & "Contents:Resources:Gif:")
        repeat
            repeat with aFile in extractedGiFFiles
                set the desktop picture to aFile
                delay 0.05
            end repeat
        end repeat
    end tell
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 2015-05-19
      • 1970-01-01
      • 2013-09-19
      • 2020-02-04
      相关资源
      最近更新 更多